[英]How to run Node.js app with ES6 features enabled?
I use the require hook of BabelJS (formerly named 6to5) to run node apps with es6features : 我使用BabelJS的require钩子 (以前命名为6to5)来运行带有es6features的节点应用程序:
// run.js
require("babel/register");
require("./app.js6");
I call node run.js
to run my app.js6 . 我调用
node run.js
来运行我的app.js6 。 I need to install BabelJS and provide a run.js for each project I'd like to use es6features. 我需要安装BabelJS,并为每一个我想使用es6features项目run.js。 I would prefer a call like
nodejs6 app.js6
. 我更喜欢像
nodejs6 app.js6
这样的调用。 How can I achieve this system independently (Unix and Windows)? 如何独立实现此系统(Unix和Windows)?
Add the babel-cli
and babel-preset-es2015
(aka ES6) dependencies to your app's package.json file and define a start
script: 将
babel-cli
和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 (eg 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. 这样做的一个好处是运行您的应用程序的命令保持不变,这有助于您与其他开发人员合作。
How configure node.js app with es6 support and server reload on file change . 如何在文件更改时配置带有es6支持和服务器重新加载的node.js应用程序 。
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 - 它也可以是阶段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: 您的项目根目录应如下所示:
7.Add to package.json needed 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.Available命令:
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重新启动节点服务器,这正在监视dist目录的更改
9.Final notes 9.Final notes
10.Run server and start creating app in src directory. 10.运行服务器并开始在src目录中创建应用程序。
npm run serve
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上提供了完整的woking样板 - https://github.com/maciejsikora/node-express-babel-boilerplate 。
您可以使用带有--harmony标志的节点来运行带有es6功能的脚本
you need to install babel-register
and babel-preset-es2015
preset Which used into babel-register
options to Enabled convert ES6
to ES5
on-the-fly transpilation 你需要安装
babel-register
和babel-preset-es2015
预设用于babel-register
选项的启用将ES6
转换为ES5
即时转换
npm install babel-register
npm install babel-preset-es2015
your run.js file: 你的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 .babelrc
file to set Babel presets
options As we setting it with require
method 注意 :现在你不需要
.babelrc
文件来设置Babel presets
选项我们用require
方法设置它
node -r babel-register scripts.js
This is the best solution 这是最好的解决方案
npx babel-node scripts.js
!Babel node doesn't work well in case of exit process and kexec
package also doesn't help in this case (as I tried) !Babel节点在退出进程的情况下不能很好地工作,在这种情况下
kexec
包也没有帮助(我试过)
In both cases you need to use .babelrc
which should describe presets and plugins for your app. 在这两种情况下,您都需要使用
.babelrc
,它应该为您的应用描述预设和插件。
npx
is using only for execution of libraries which are not installed with npm
or yarn
. npx
仅用于执行未安装npm
或yarn
的库。 Otherwise you need to npm i -g babel-cli
and then babel-node script.js
否则你需要
npm i -g babel-cli
然后babel-node script.js
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
使用
node es6 thefile.js
运行es6精选脚本
As of babel 6, you now must install babel-register
and use the following 从babel 6开始,您现在必须安装
babel-register
并使用以下内容
require("babel-register");
Be sure to also install the babel es2015 preset. 务必安装babel es2015预设。
Refer this: 请参考:
https://stackoverflow.com/a/51485027/1549191 https://stackoverflow.com/a/51485027/1549191
or this boilerplate: 或者这个样板:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.