- 类似
(node:11340) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.的错误比较常见。 - 解决方法:只需在
package.json中添加 "type": "module"行,告诉 Node.js 应用程序应该以 ECMAScript 模块(ESM)的方式处理js文件:
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"type": "module", // 添加此行
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
}
}
- 在
package.json 文件中指定 "type": "module",这样就可以在项目中使用 ES6 模块语法(即 import 和 export,import 和 export 是 ES6 模块系统的核心,实现在不同的 JavaScript 文件中共享代码。)。