Javascript 如何在启用 ES6 功能的情况下运行 Node.js 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28782656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Node.js app with ES6 features enabled?
提问by maiermic
I use the require hookof BabelJS(formerly named 6to5) to run node apps with es6features:
我用的是需要挂钩的BabelJS(原名6to5)与运行节点的应用程序es6features:
// run.js
require("babel/register");
require("./app.js6");
I call node run.jsto run my app.js6. I need to install BabelJS and provide a run.jsfor 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)?
我打电话node run.js来运行我的app.js6。我需要安装 BabelJS 并为我想使用 es6features 的每个项目提供一个run.js。我更喜欢像nodejs6 app.js6. 我如何独立实现这个系统(Unix 和 Windows)?
回答by X?pplI'-I0llwlg'I -
Add the babel-cliand babel-preset-es2015(aka ES6) dependencies to your app's package.json file and define a startscript:
将babel-cliand babel-preset-es2015(又名 ES6)依赖项添加到您的应用程序的 package.json 文件并定义一个start脚本:
{
"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:
如果你决定停止使用 Babel(例如,一旦 Node.js 支持所有 ES6 特性),你可以从 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.
这样做的一个好处是运行您的应用程序的命令保持不变,这在您与其他开发人员合作时会有所帮助。
回答by Maciej Sikora
How configure node.js app with es6 support and server reload on file change.
如何使用 es6 支持和服务器重新加载文件更改配置node.js 应用程序。
I.Configuration steps ( creating project from the scratch ):
一、配置步骤(从头开始创建项目):
1.Go in terminal to Your project main directory
1.进入终端到你的项目主目录
npm init//create package.json for project
npm init//为项目创建package.json
2.Install dependencies
2.安装依赖
npm install --save-dev babel
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0 //*1
npm install --save-dev nodemon
1 - it can be also stage-1 or 2, it depends what features of es We want to use
1 - 它也可以是 stage-1 或 2,这取决于我们想使用 es 的哪些特性
3.We should have in package.json file something like that ( for sure package version will be different but it is ok ):
3.我们应该在 package.json 文件中有类似的东西(当然包版本会有所不同,但没关系):
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.16.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"nodemon": "^1.11.0"
}
4.Create .babelrc file in root project directory ( there is package.json file )
4.在项目根目录下创建.babelrc文件(有package.json文件)
{
"presets": ["es2015", "stage-0"]
}
5.Create two directories:
5.创建两个目录:
src- here is working directory with files writen in es6
src- 这里是用 es6 编写的文件的工作目录
dist- here files will compile to es5 using babel
dist- 这里的文件将使用 babel 编译为 es5
Your project root directory should look like this:
您的项目根目录应如下所示:
- project
- src
- index.js //main project file
- dist
- package.json
- .babelrc
- src
- 项目
- 源文件
- index.js //主项目文件
- 区
- 包.json
- .babelrc
- 源文件
7.Add to package.jsonneeded commands:
7.添加到package.json需要的命令:
"scripts": {
"watch": "babel -w src/ -d dist/",
"build": "babel src/ -d dist/",
"serve": "babel -w src/ -d dist/ | nodemon --watch dist",
"test": "echo \"Error: no test specified\" && exit 1"
}
8.Available commands:
8.可用命令:
npm run watch//starts watch watch changes in src directory and compiles in to dist
npm run watch//开始观察src目录下的变化并编译到dist
npm run build//compiles files from src directory to dist
npm run build//从src目录编译文件到dist
npm run serve//it is doing watch + start node server, on every file change it will restart node server using nodemon which is watching dist directory changes
npm run serve//它正在执行 watch + start node server,在每次文件更改时,它将使用 nodemon 重新启动 node server,nodemon 正在监视 dist 目录更改
9.Final notes
9.最后的笔记
- Server will run dist/index.js file as main file.
- File dist/index.js will be compiled from src/index.js so there should be main file of project.
- dist directory should be added to ignore by git ( but not ignore it for npm if it will be a node package )
- 服务器将运行 dist/index.js 文件作为主文件。
- 文件 dist/index.js 将从 src/index.js 编译,所以应该有项目的主文件。
- dist 目录应该被 git 添加到忽略(但如果它是一个节点包,则不要忽略它对于 npm )
10.Run server and start creating app in srcdirectory.
10.运行服务器并开始在src目录中创建应用程序。
npm run serve
II. Easier way ( ready to use boilerplate )
二、更简单的方法(准备使用样板)
If it is too many points for You then full woking boilerplate is available on github - https://github.com/maciejsikora/node-express-babel-boilerplate.
如果它对你来说太多了,那么 github 上提供了完整的样板文件 - https://github.com/maciejsikora/node-express-babel-boilerplate。
回答by Safi
You can use node with --harmony flag to run script with es6 features
您可以使用带有 --harmony 标志的节点来运行具有 es6 功能的脚本
回答by SerzN1
node -r babel-register scripts.js
node -r babel-register scripts.js
This is the best solution
这是最好的解决方案
npx babel-node scripts.js
npx babel-node scripts.js
!Babel node doesn't work well in case of exit process and kexecpackage also doesn't help in this case (as I tried)
!Babel 节点在退出过程和kexec包的情况下无法正常工作在这种情况下也无济于事(正如我所尝试的那样)
In both cases you need to use .babelrcwhich should describe presets and plugins for your app.
在这两种情况下,您都需要使用.babelrcwhich 来描述您的应用程序的预设和插件。
npxis using only for execution of libraries which are not installed with npmor yarn. Otherwise you need to npm i -g babel-cliand then babel-node script.js
npx仅用于执行未安装npm或的库yarn。否则你需要npm i -g babel-cli然后babel-node script.js
回答by ahmed hamdy
you need to install babel-registerand babel-preset-es2015preset Which used into babel-registeroptions to Enabled convert ES6to ES5on-the-fly transpilation
您需要安装babel-register和babel-preset-es2015预设 which used into babel-registeroptions to Enabled convert ES6to ES5on-the-fly transpilation
npm install babel-register
npm install babel-preset-es2015
your run.jsfile:
你的run.js文件:
// require babel-register and set Babel presets options to es2015
require('babel-register')({
presets: [ 'es2015' ]
});
require("./app.js6");
Notice: Now you does not need .babelrcfile to set Babel presetsoptions As we setting it with requiremethod
注意:现在您不需要.babelrc文件来设置Babel presets选项,因为我们使用require方法设置它
回答by Jiahao D.
I would prefer a call like
nodejs6 app.js6.
我更喜欢像
nodejs6 app.js6.
You may try the wrapper solution with babel-core api:
您可以尝试使用 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
运行你的 es6 特色脚本 node es6 thefile.js
Reference: offical usage doc
参考:官方使用文档
回答by Chiedo
As of babel 6, you now must install babel-registerand use the following
从 babel 6 开始,您现在必须安装babel-register和使用以下内容
require("babel-register");
Be sure to also install the babel es2015 preset.
确保还安装了 babel es2015 预设。
回答by Priyanshu Chauhan
Refer this:
参考这个:
https://stackoverflow.com/a/51485027/1549191
https://stackoverflow.com/a/51485027/1549191
or this boilerplate:
或者这个样板:

