I use the require hook of BabelJS (formerly named 6to5) to run node apps with es6features:
// run.js
require(\"babel/register\");
require(\"./app.js6\");
How configure node.js app with es6 support and server reload on file change.
1.Go in terminal to Your project main directory
npm init
//create package.json for project
2.Install dependencies
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
3.We should have in package.json file something like that ( for sure package version will be different but it is ok ):
"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 )
{
"presets": ["es2015", "stage-0"]
}
5.Create two directories:
src - here is working directory with files writen in es6
dist - here files will compile to es5 using babel
Your project root directory should look like this:
7.Add to package.json needed commands:
"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:
npm run watch
//starts watch watch changes in src directory and compiles in to dist
npm run build
//compiles files from src directory to 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
9.Final notes
10.Run server and start creating app in src directory.
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.
You can use node with --harmony flag to run script with es6 features
I would prefer a call like
nodejs6 app.js6
.
You may try the wrapper solution with 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
Reference: offical usage doc
As of babel 6, you now must install babel-register
and use the following
require("babel-register");
Be sure to also install the babel es2015 preset.
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
npm install babel-register
npm install babel-preset-es2015
your run.js file:
// 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
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)
In both cases you need to use .babelrc
which should describe presets and plugins for your app.
npx
is using only for execution of libraries which are not installed with npm
or yarn
. Otherwise you need to npm i -g babel-cli
and then babel-node script.js