在节点4.x中导出ES6类意外的保留字
我有一个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;
ES6模块在Node中不稳定,但可以使用--harmony_modules
启用它们。 这显然不build议在生产环境中使用。
节点4.x中的ES6支持