问题:
m1.js:
let a = 10;
let b = 20;
let c = 30;
function show() {
console.log(1111);
}
export default {
a,
c,
show
}
index.js:
import m1 from './m1.js'
console.log(m1);
运行:
npx babel-node .\index.js
报错:
Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.
import m1 from ‘./m1.js’;
^^^^^^
SyntaxError: Cannot use import statement outside a module
解决:
输入命令:
1.npm init -y
2.在 package.json 中添加字段 type
再次运行:
{ a: 10, c: 30, show: [Function: show] }