How to run Node.js app with ES6 features enabled?

后端 未结 8 1958
[愿得一人]
[愿得一人] 2020-12-02 06:31

I use the require hook of BabelJS (formerly named 6to5) to run node apps with es6features:

// run.js
require(\"babel/register\");
require(\"./app.js6\");
         


        
8条回答
  •  执念已碎
    2020-12-02 06:50

    I would prefer a call like nodejs6 app.js6.

    You may try the wrapper solution with babel-core api:

    // Save as es6.js
    
    var babel = require("babel-core");
    var argc = process.argv.length;
    
    babel.transformFile(process.argv[argc - 1], function (err, result) {
        eval(result.code);
    });
    

    Run your es6 featured script with node es6 thefile.js

    Reference: offical usage doc

提交回复
热议问题