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

后端 未结 8 1827
[愿得一人]
[愿得一人] 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 07:09

    Add the babel-cli and babel-preset-es2015 (aka ES6) dependencies to your app's package.json file and define a start script:

    {
      "dependencies": {
        "babel-cli": "^6.0.0",
        "babel-preset-es2015": "^6.0.0"
      },
      "scripts": {
        "start": "babel-node --presets es2015 app.js"
      }
    }
    

    Then you can simply execute the following command to run your app:

    npm start
    

    If you ever decide to stop using Babel (e.g. once Node.js supports all ES6 features), you can just remove it from package.json:

    {
      "dependencies": {},
      "scripts": {
        "start": "node app.js"
      }
    }
    

    One benefit of this is that the command to run your app remains the same, which helps if you are working with other developers.

    0 讨论(0)
  • 2020-12-02 07:11

    Refer this:

    https://stackoverflow.com/a/51485027/1549191

    or this boilerplate:

    Boilerplate: node-es6

    0 讨论(0)
提交回复
热议问题