错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块? [英] Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12?
本文介绍了错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自 https://2ality.com/2019/04/nodejs-esm-impl.html Node 12 应该支持 es6 模块;但是,我只是不断收到错误:
From https://2ality.com/2019/04/nodejs-esm-impl.html Node 12 should support es6 modules; however, I just keep getting the error:
问题:如何在 node 12 中使用 es6 模块制作 MVP?
Question: How do I make a MVP of using es6 modules in node 12?
package.json
{
"name": "dynamic-es6-mod",
"version": "1.0.0",
"description": "",
"main": "src/index.mjs",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node src/index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"globby": "^10.0.1"
}
}
$ node -v
$ 12.6.0
$ npm run start
internal/modules/cjs/loader.js:821
throw new ERR_REQUIRE_ESM(filename);
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/dev/dynamic-es6-mod/src/index.mjs
at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:821:9)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at internal/main/run_main_module.js:17:11
推荐答案
你所要做的就是添加标志 --experimental-modules 支持新的 es6 导入/导出语句以及命令很重要,如下所示.
All you have to do is adding the flag --experimental-modules that supports the new es6 import/export statement also the order is important as the following.
"start": "node --experimental-modules src/index.mjs "
这篇关于错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文