[英]Error [ERR_REQUIRE_ESM]: Must use import to load ES module
I am super new to javascript, and a file app.js I am trying to run with npm looks like this:我是 javascript 的超级新手,我尝试使用 npm 运行的文件 app.js 如下所示:
import {placeOnWallComponent, wallFromFloorComponent} from './wall-from-floor'
AFRAME.registerComponent('place-on-wall', placeOnWallComponent)
AFRAME.registerComponent('wall-from-floor', wallFromFloorComponent)
And when I change the file type to app.mjs instead of app.mjs, I get another error: "Must use import to load ES Module."当我将文件类型更改为 app.mjs 而不是 app.mjs 时,我收到另一个错误:“必须使用导入来加载 ES 模块。”
I also have this as my package.json我也有这个作为我的 package.json
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "app.js",
"@std/esm": "cjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.mjs"
},
"author": "",
"license": "ISC"
}
How do I fix this error?如何修复此错误?
UPDATE: It's still happening even after I changed the files to.mjs.更新:即使我将文件更改为.mjs,它仍然会发生。 If I try having start be just 'app.mjs' instead of 'node app.js', I get:
如果我尝试开始只是'app.mjs'而不是'node app.js',我会得到:
'this file does not have an app associated with it' '此文件没有与之关联的应用程序'
Add添加
"type": "module"
in your package.json在你的 package.json
change file extensions ".js" to ".mjs" and try again the command将文件扩展名“.js”更改为“.mjs”并重试该命令
node --experimental-modules app.mjs
节点 --experimental-modules app.mjs
This is wrong "@std/esm": "cjs",
这是错误的
"@std/esm": "cjs",
@std/esm
is depecated. @std/esm
esm 已弃用。 Remove this and run npm install esm
on the commandline to install the esm package instead.删除它并在命令行上运行
npm install esm
来安装 esm package。
Adjust your load script to "start": "node -r esm app.mjs"
to load esm when your app runs.将加载脚本调整为
"start": "node -r esm app.mjs"
以在应用运行时加载 esm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.