简体   繁体   English

在节点4.x中导出ES6类意外的保留字

[英]export ES6 class in Node 4.x Unexpected reserved word

I have the following in a Node scripts: 我在Node脚本中有以下内容:

"use strict";

class Whatever {
    constructor() {
        console.log("I'm in the constructor!");
    }
}

export default Whatever;

I get Unexpected reserved word regarding export . 我得到了关于export Unexpected reserved word

What am I missing here? 我在这里错过了什么? How do you specify a class definition in an external file and include/require it? 如何在外部文件中指定类定义并包含/要求它?

Node.js doesn't support ES6 modules by default. Node.js默认不支持ES6模块。 You would need to activate them with the --harmony or --harmony_modules flag. 您需要使用--harmony--harmony_modules标志激活它们。 Default ist the CommonJS declaration ( require / module.exports ) . 默认是CommonJS声明require / module.exports

Modify your code to support the CommonJS syntax: 修改代码以支持CommonJS语法:

"use strict";

class Whatever {
    constructor() {
        console.log("I'm in the constructor!");
    }
}

module.exports = Whatever;

ES6 modules aren't stable in Node yet, but you can use --harmony_modules to enable them. ES6模块在Node中不稳定,但您可以使用--harmony_modules来启用它们。 This obviously is not recommended in a production environment. 显然不建议在生产环境中使用。

ES6 support in Node 4.x Node 4.x中的ES6支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM