export ES6 class in Node 4.x Unexpected reserved word

后端 未结 2 732
抹茶落季
抹茶落季 2021-01-02 10:02

I have the following in a Node scripts:

\"use strict\";

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

exp         


        
2条回答
  •  青春惊慌失措
    2021-01-02 10:52

    Node.js doesn't support ES6 modules by default. You would need to activate them with the --harmony or --harmony_modules flag. Default ist the CommonJS declaration (require/module.exports).

    Modify your code to support the CommonJS syntax:

    "use strict";
    
    class Whatever {
        constructor() {
            console.log("I'm in the constructor!");
        }
    }
    
    module.exports = Whatever;
    

提交回复
热议问题