【问题标题】:Node - unexpected identifier节点 - 意外标识符
【发布时间】:2018-06-11 08:02:27
【问题描述】:

我正在尝试使用 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');

节点版本 - 10.4.0(节点设置很好)
当我运行 node 1.js 时 - 出现错误 Unexpected identifier,指向狗。这里有什么问题?

P.S. 1.js 已更新以正确使用导入的函数,但即使在此之后我仍然遇到错误。

【问题讨论】:

  • @RobC,仍然。实际上问题出在我想的其他地方,因为在 import 语句中指向 dog 的错误。

标签: node.js ecmascript-6 functional-programming


【解决方案1】:

您的代码有效,它会记录:

狗说:111

但是,ECMAScript 模块在节点 v10.4.0 中是 Experimental

您需要使用--experimental-modules 标志/选项运行节点。例如

node --experimental-modules 1.js 

另请参阅有关模块文件的 .mjs 扩展名的说明。因此,您可能需要将1.js 更改为:

// Note the .mjs extension
import dog from './dog.mjs';

const d = dog()
d.test('111');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 2017-06-19
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2018-01-17
    • 2019-02-24
    • 2015-10-01
    相关资源
    最近更新 更多