簡體   English   中英

在節點4.x中導出ES6類意外的保留字

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

我在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來啟用它們。 顯然不建議在生產環境中使用。

Node 4.x中的ES6支持

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM