Node.js中使用ES6报错

背景

index.js

import hello from './cpoy';
hello();

cpoy/index.js

const hello = () => {
    console.log("hello world!")
}
export default hello

package.json 中添加了"type": "module"配置。

报错

Directory import xxxx is not supported resolving ES modules imported from xxxx

解决

正确导入方式为import hello from './cpoy'/index.js,而不是import hello from './cpoy';不要忘了.js

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