我已经开始在一个基于 Node.js 的现有项目上工作。我只是想了解执行流程,在那里我遇到了一些*.mjs文件。我搜索了网络,发现这些是基于模块的 JS 文件。
我想知道它与*.js文件有什么不同(它有什么好处)?
我创建了 Nest js 项目,如下所示。
nest new project-name
并从 nuxt3 导入以下内容,其类型为module带有文件扩展名的 Node js mjs(导入的类型定义不需要编写 mjs)。
import { ViteBuildContext, ViteOptions, bundle } from '@nuxt/vite-builder-edge';
它给了我以下错误。
Uncaught Error Error [ERR_REQUIRE_ESM]: require() of ES Module c:\project\node_modules\@nuxt\vite-builder-edge\dist\index.mjs not supported. Instead change the require of c:\project\node_modules\@nuxt\vite-builder-edge\dist\index.mjs to a dynamic import() which is available in all CommonJS modules.
所以我尝试添加"type": "module"package.json 所以现在我收到以下错误。
Uncaught ReferenceError ReferenceError: exports is not defined in ES module scope This file is being treated as an ES module because …
(我知道这听起来可能类似于_nuxt 文件夹中缺少 js 文件,但不幸的是,我无法理解那里的答案)
当我将我的dist文件夹部署到 GitHub Pages 时,它包含
dist
| _nuxt
| css/main.css
| entry.*******.css
| entry-*******.mjs
| index-*******.mjs
| history-********.mjs
| header-********.mjs
| ... some other mjs-files
| css/main.css
| index.html
| history.html
| ... some other HTML-files
Run Code Online (Sandbox Code Playgroud)
HTML 页面已提供,非常好,并且在<head>- 部分中,他们想要加载模块(.mjs- 文件)。不幸的是,所有这些请求都失败并返回 404:
为什么对 -fold 的请求_nuxt失败,而/和 的/css请求却通过?
编辑:刚刚看到在 VS Code 中,该文件夹只是标记为-folder的符号链接.output/public(由 生成nuxi generate):
这可能是问题所在吗?但无论如何,它似乎包含这些文件:
编辑 II:我无法运行npm run …
过去,app-module-path每当我想在 Node.js 应用程序中使用相对路径时,我都会使用它。如果我通过该格式使用 ES 模块.mjs,如何在某个目录路径变为相对路径的情况下具有相同的功能?
以另一种方式,我是否能够为目录分配一个别名,以便所有相对路径都相对于该别名,就像./路径的别名相对于当前目录一样。
我可以使用--experimental-modules标志运行带有 nodejs 的 mjs 文件。
node --experimental-modules index.mjs
Run Code Online (Sandbox Code Playgroud)
包.json:
{
"name": "mjs-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^2.4.2",
"uuid": "^3.3.2"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
Run Code Online (Sandbox Code Playgroud)
和 index.mjs
{
"name": "mjs-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^2.4.2",
"uuid": "^3.3.2"
},
"devDependencies": {
"nodemon": "^1.19.1"
} …Run Code Online (Sandbox Code Playgroud) 我有一个后端 CJS 应用程序和一个前端 ESM 应用程序。以及我在 ESM 中创建的节点模块。节点模块非常适合我的 ESM 应用程序,因为它们都使用 ESMimport语法。尝试在 CJS 应用程序中使用它显然会引发错误,因为它无法读取 ESMimport语法。
我尝试使用Rollup.js将ESM代码转换为并在我的文件中CJS使用,但这不起作用。conditional exportspackage.json
似乎不可能eval在 Node.js ES6 中使用创建变量,但我不明白为什么。这在 CentOS 7 上发生在我身上,但我不认为操作系统是这里的问题。
常规 Node.js 文件(test.js):
eval("var a=1");
console.log(a);
Run Code Online (Sandbox Code Playgroud)
使用 .mjs 扩展名制作相同的文件以与 Node.js ES6 (test.mjs) 一起运行:
eval("var a=1");
console.log(a);
Run Code Online (Sandbox Code Playgroud)
之后,使用 Node.js 和 Node.js ES6 运行 2 个文件:
$ node test.js
1
$ node --experimental-modules test.mjs
(node:9966) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: a is not defined
at file:///temp/test.mjs:2:13
at ModuleJob.run (internal/modules/esm/module_job.js:96:12)
Run Code Online (Sandbox Code Playgroud)
它是与 ES6 相关的问题吗?我在浏览器的控制台上试过,问题是一样的:
>> eval("var a=1"); console.log(a);
1
>> class c { static f(){ eval("var a=1"); console.log(a); } }
c.f()
ReferenceError: …Run Code Online (Sandbox Code Playgroud) 我目前正在使用带有.mjs扩展的ES6 模块并为某些功能创建测试用例。
我之所以选择AVA它,是因为它支持这种扩展类型,但测试执行没有按预期运行。
我认为脚本没有正确转换,或者我在我的 package.json
我感谢任何有使用 AVA 经验的人的任何帮助 --experimental-modules
包.json
{
"scripts": {
"test": "ava --init"
},
"ava": {
"require": [
"esm"
],
"babel": false,
"extensions": [
"mjs"
]
}
}
Run Code Online (Sandbox Code Playgroud)
test.spec.mjs
import rotate from './index.mjs'
import test from 'ava';
test('rotate img', t => {
var m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
rotate(m);
t.is(m, [[7, 4, 1], [8, 5, 2], [9, 6, 3]]);
});
Run Code Online (Sandbox Code Playgroud)
索引.js
var rotate =function(matrix) {
let cols …Run Code Online (Sandbox Code Playgroud) mjs ×8
node.js ×7
javascript ×5
nuxtjs3 ×2
ava ×1
commonjs ×1
ecmascript-6 ×1
es6-modules ×1
eval ×1
github-pages ×1
nodemon ×1
nuxt.js ×1
rollupjs ×1
typescript ×1
unit-testing ×1