node.js - Node - 意外标识符

标签 node.js ecmascript-6 functional-programming

我正在尝试使用 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/

相关文章:

r - 使用数据框的列作为使用 pmap 的函数的参数

javascript - 错误: Corrupted zip : can't find end of central directory - XLSX

javascript - 连接到远程 SSH 服务器(通过 Node.js/html5 控制台)

javascript - 单击按钮时,使用reactjs更改单击按钮以及该组中其他按钮的类名

node.js - 我可以在 Node.js 应用程序中使用 ES6 模块并在 Visual Studio 中对其进行调试吗?

javascript - 如何将 Immutable JS 与类型化的 ES6 类一起使用?

c - c中的指针和数组

functional-programming - 具有相同字段的两条记录的模式匹配

javascript - 正则表达式测试在 nodejs 中失败了吗?

javascript - PKCS#12 (.pfx .p12) 使用 node.js 生成?