gpt4 book ai didi

javascript - 错误 [ERR_REQUIRE_ESM] : How to use es6 modules in node 12?

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:25 27 4
gpt4 key购买 nike

来自https://2ality.com/2019/04/nodejs-esm-impl.html Node 12应该支持es6模块;但是,我不断收到错误:

问题:如何在 Node 12 中使用 es6 模块制作 MVP?

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

最佳答案

official documentation对于模块状态,v2 应与 require() 一起使用。

有一个解决方法。相反,它可以异步加载:

const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

关于javascript - 错误 [ERR_REQUIRE_ESM] : How to use es6 modules in node 12?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57169793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com