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)?
Refer this:
https://stackoverflow.com/a/51485027/1549191
or this boilerplate:
Boilerplate: node-es6
You can use node with --harmony flag to run script with es6 features
you need to install
babel-register
andbabel-preset-es2015
preset Which used intobabel-register
options to Enabled convertES6
toES5
on-the-fly transpilationyour run.js file:
Notice: Now you does not need
.babelrc
file to setBabel presets
options As we setting it withrequire
methodYou may try the wrapper solution with babel-core api:
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 followingBe sure to also install the babel es2015 preset.
Add the
babel-cli
andbabel-preset-es2015
(aka ES6) dependencies to your app's package.json file and define astart
script:Then you can simply execute the following command to run your app:
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:
One benefit of this is that the command to run your app remains the same, which helps if you are working with other developers.