我正在尝试使用 Node 和一些 ES6/函数式的东西。
这里有两个文件。
dog.js
const dog = () => {
return {
test: (arg) => console.log("dog say: " + arg)
}
}
export default dog;
1.js
import dog from './dog';
const d = dog()
d.test('111');
Node 版本 - 10.4.0( Node 设置没问题)
当我运行 node 1.js
- 出现错误 Unexpected identifier
,指向狗。这里有什么问题?
P.S. 1.js 已更新以正确使用导入函数,但即使在那之后我仍然遇到错误。
最佳答案
您的代码有效,它会记录:
dog say: 111
但是,ECMAScript 模块是 Experimental在 Node v10.4.0 中。
您需要使用 --experimental-modules
标志/选项运行 Node 。例如
node --experimental-modules 1.js
另请参阅有关模块文件的 .mjs 扩展名的注释。所以你可能需要将 1.js
更改为:
// Note the .mjs extension
import dog from './dog.mjs';
const d = dog()
d.test('111');
关于node.js - Node - 意外标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792976/