欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.

程序员文章站 2022-05-19 10:58:47
...

因为需要测试mockjs的代码,在vscode中创建了一个文件夹,使用npm install mockjs 安装mockjs, Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.

//01.js
import Mock from 'mockjs'
const data = Mock.mock({
  'list|1-10': [{
    'id|+1': 1
  }]
})
console.log(data);
console.log(JSON.stringify(data,null,1));  //格式化的对象,替换的对象,空格

在控制台运行node 01.js
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
原因:为了加载ES模块,需要在package.json中设置“type”:“module” 或者使用.mjs扩展。可是我的文件夹下面没有package.jsonWarning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
解决方法:npm init,在生成的package.json中,添加"type": “module”
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.

Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.

//package.json
{
  "name": "22_mockjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",   //加上这个
  "dependencies": {
    "mockjs": "^1.1.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "wyj",
  "license": "ISC"
}

再次运行js代码就可以了
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.

Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.

参考文章

相关标签: nodejs json