npx babel-node .\index.js :Cannot use import statement outside a module

问题:

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
npx babel-node .\index.js :Cannot use import statement outside a module_第1张图片

再次运行:

{ a: 10, c: 30, show: [Function: show] }

运行成功

你可能感兴趣的:(Node.js,node.js)