How to run a Node.js application with ES6 features enabled?

I use the BabelJS interceptor (previously called 6to5) to run node applications with es6features :

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

I call node run.js to launch my app.js6 . I need to install BabelJS and provide run.js for each project in which I would like to use es6features. I would prefer a call like nodejs6 app.js6 . How can I reach this system on my own (Unix and Windows)?

+63
javascript ecmascript-6 babeljs
Feb 28 '15 at 14:10
source share
8 answers

Add the dependencies babel-cli and babel-preset-es2015 (aka ES6) to your application 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 run the following command to launch your application:

 npm start 

If you decide to stop using Babel (for example, once Node.js supports all ES6 features), you can simply remove it from package.json:

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

One of the advantages of this is that the launch command for your application remains the same, which helps if you work with other developers.

+122
Apr 02 '15 at 14:20
source share

How to configure a node.js application with es6 support and server reboot when a file changes .




I. Configuration steps (creating a project from scratch):

1.Go in the terminal to the main directory of your project

npm init // create package.json for the 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 stage 1 or 2, it depends on what functions we want to use

3. We should have something like this in the package.json file (the version of the package will probably be different, but this is normal):

 "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 a .babelrc file in the project root directory (there is a package.json file)

 { "presets": ["es2015", "stage-0"] } 

5. Create two directories:

src is the working directory with files written to es6

dist - here the files will be compiled for es5 using babel

The root directory of the project should look like this:

  • Project
    • CSI
      • index.js // main project file
    • distance
    • package.json
    • .babelrc

7. Add the necessary commands to 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:

npm run watch // starts watch viewing in src directory and compiles to dist

npm run build // compiles files from src directory to dist

npm run serve // it starts watch + start node server, every time the file changes, it restarts the node server using nodemon, which tracks changes to the dist directory

9. Final notes

  • The server will run the dist / index.js file as the main file.
  • The dist / index.js file will be compiled from src / index.js, so there should be a main project file.
  • the dist directory must be added to ignore using git (but not ignore it for npm if it is node).

10. Start the server and run the application in the src directory.

 npm run serve 



II. Easy way (ready to use template)

If you have too many points, then the full complete woking scheme is available on github - https://github.com/maciejsikora/node-express-babel-boilerplate .

+35
Oct 20 '16 at 8:21
source share

You can use node with the -harmony flag to run a script with es6 functions

+16
Feb 28 '15 at 16:53
source share

you need to install babel-register and babel-preset-es2015 , which is used in the babel-register to Enabled ES6 to convert ES6 to ES5 broadcast on the fly

  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"); 

Note Now you do not need a .babelrc file to set Babel presets options. When we install it using the require method

+6
Jan 25 '16 at 21:06
source share
  1. node -r babel-register scripts.js

This is the best solution.

  1. npx babel-node scripts.js

Knot! Babel does not work kexec on exit, and the kexec package also does not help in this case (as I tried)

In both cases, you need to use .babelrc which should describe presets and plugins for your application.

npx uses only to run libraries that are not installed with npm or yarn . Otherwise, you need to run npm -g babel-cli and then babel-node script.js

+6
May 05 '18 at 11:40
source share

I would prefer a call like nodejs6 app.js6 .

You can try shell solution using 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 with a script function using node es6 thefile.js

Link : official use of doc

+3
Jul 20 '15 at 9:43
source share

Starting with babel 6, you should install babel-register and use the following

 require("babel-register"); 

Be sure to install the babel es2015 preset.

+1
Dec 26 '15 at 15:58
source share

Recommend this:

stack overflow

or this template:

Boilerplate: node-es6

+1
Jul 23 '18 at 18:20
source share



All Articles