node运行含有ES6语法的js文件

当使用node运行以下代码时

import { Node, LinkedList } from "./07.js"
node index.js

会报错提示:

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
错误:加载ES模块,设置"type": "module" 在packahejson中或使用.mjs文件

在packahejson中设置"type": "module" 

先创建package.json,在package.json中加入"type": "module"

npm init

package.json

{
  "name": "js",
  "version": "1.0.0",
  "description": "",
  "main": "00-test.js",
  "type": "module",        // 加入这句
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

 

posted @ 2021-08-07 16:12  邢韬  阅读(227)  评论(0编辑  收藏  举报