节点-意外标识符 [英] Node - unexpected identifier
问题描述
我正在尝试使用Node和一些ES6/功能性的东西.
这是两个文件.
dog.js
I'm trying to play around with Node and some ES6/functional stuff.
Here is two files.
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');
节点版本-10.4.0(可以使用节点设置)
当我运行node 1.js
时-出现错误 Unexpected identifier
,指向了dog.怎么了?
Node version - 10.4.0 (Node settings are fine)
When I'm running node 1.js
- getting error Unexpected identifier
, pointing on dog. What is wrong here?
P.S..1.js已更新,可正确使用导入的功能,但即使在此之后,我仍然遇到错误.
P.S. 1.js was updated for proper usage of imported function, but even after that I'm still getting errors.
推荐答案
您的代码有效,它记录:
Your code works, it logs:
狗说:111
dog say: 111
但是,ECMAScript模块在实验中节点v10.4.0.
However, ECMAScript Modules are Experimental in node v10.4.0.
您需要使用--experimental-modules
标志/选项运行节点.例如
You'll need to run node with the --experimental-modules
flag/option. For example
node --experimental-modules 1.js
另请参阅有关模块文件.mjs扩展名的注释.因此,您可能需要将1.js
更改为:
Also see the note regarding .mjs extension for module file(s). So you'll probably need to change 1.js
to:
// Note the .mjs extension
import dog from './dog.mjs';
const d = dog()
d.test('111');
这篇关于节点-意外标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!