简体   繁体   English

错误 [ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES 模块

[英]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.

相关问题 webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - 但我用的是 import! - webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - But I AM using import! SSR with Node, Webpack, React, Express, Material UI, React Router - 错误 [ERR_REQUIRE_ESM]: 必须使用导入加载 ES 模块 - SSR with Node, Webpack, React, Express, Material UI, React Router - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module 升级 Node JS 和 ERR_REQUIRE_ESM:必须使用 import 加载 ES Module: 'inheritsloose.js' of Babel Runtime - Upgrading Node JS and ERR_REQUIRE_ESM: Must use import to load ES Module: 'inheritsloose.js' of Babel Runtime 将 D3.js 7.0.0 与 Next.js 11.0.1 一起使用时,如何解决“[ERR_REQUIRE_ESM]:必须使用导入加载 ES 模块”的问题? - How do I resolve "[ERR_REQUIRE_ESM]: Must use import to load ES Module" when using D3.js 7.0.0 with Next.js 11.0.1? 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module 在 Next.js ERR_REQUIRE_ESM 中导入 ES 模块 - Import ES module in Next.js ERR_REQUIRE_ESM PM2 带 ES 模块。 错误:ERR_REQUIRE_ESM - PM2 with ES module. Error: ERR_REQUIRE_ESM 错误 [ERR_REQUIRE_ESM]: 使用 SSR (Inertiajs) 时需要 ES 模块 - Error [ERR_REQUIRE_ESM]: require() of ES Module while using SSR (Inertiajs) 错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块? - Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12? 错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM