如何运行启用ES6功能的Node.js应用程序? [英] How to run Node.js app with ES6 features enabled?

查看:154
本文介绍了如何运行启用ES6功能的Node.js应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 BabelJS的需要钩子 (以前称为6to5)以 es6features 运行节点应用程序:

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

我调用节点run.js 运行我的 app.js6 。我需要安装BabelJS,并为每个我想使用es6features的项目提供一个 run.js 。我更喜欢像 nodejs6 app.js6 这样的电话。如何独立实现这个系统(Unix和Windows)?

解决方案

添加 babel-cli babel-preset-es2015 (又称ES6)依赖于您的应用程序的package.json文件并定义一个开始脚本:

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

然后您可以简单地执行以下命令来运行您的应用程序:

  npm start 
/ pre>

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

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

其中一个好处是那个c ommand运行您的应用程序保持不变,这有助于您与其他开发人员合作。


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

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

I call node run.js to run my app.js6. I need to install BabelJS and provide a run.js for each project I'd like to use es6features. I would prefer a call like nodejs6 app.js6. How can I achieve this system independently (Unix and Windows)?

解决方案

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.

这篇关于如何运行启用ES6功能的Node.js应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆