我在 Node 脚本中有以下内容:
"use strict";
class Whatever {
constructor() {
console.log("I'm in the constructor!");
}
}
export default Whatever;
我得到关于 export
的 Unexpected reserved word
。
我在这里缺少什么?如何在外部文件中指定类定义并包含/需要它?
最佳答案
Node.js 默认不支持 ES6 模块。您需要使用 --harmony
或 --harmony_modules
标志来激活它们。默认是 CommonJS 声明 (require
/module.exports
) .
修改您的代码以支持 CommonJS 语法:
"use strict";
class Whatever {
constructor() {
console.log("I'm in the constructor!");
}
}
module.exports = Whatever;
关于javascript - 在 Node 4.x 中导出 ES6 类意外的保留字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155917/