且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在启用 ES6 功能的情况下运行 Node.js 应用程序?

更新时间:2022-11-03 15:13:19

babel-clibabel-preset-es2015(又名 ES6)依赖项添加到您的应用程序的package.json 文件并定义一个 start 脚本:

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

如果您决定停止使用 Babel(例如,一旦 Node.js 支持所有 ES6 功能),您可以将其从 package.json 中删除:

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.