Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

// 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)?

like image 611
maiermic Avatar asked Feb 28 '15 14:02

maiermic


People also ask

What version of Node supports ES6?

While support for ES6 modules arrived as an experimental feature in Node. js 8.5, there are two ways to use these modules on earlier Node. js implementations. One method is to use the Babel transpiler to rewrite ES6 code so it can execute on older Node.


2 Answers

Add the babel-cli and babel-preset-es2015 (aka ES6) dependencies to your app's 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 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:

{   "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.

like image 195
XåpplI'-I0llwlg'I - Avatar answered Sep 23 '22 15:09

XåpplI'-I0llwlg'I -


How configure node.js app with es6 support and server reload on file change.


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

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:

  • project
    • src
      • index.js //main project file
    • dist
    • package.json
    • .babelrc

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

  • 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 )

10.Run server and start creating app in src directory.

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.

like image 27
Maciej Sikora Avatar answered Sep 24 '22 15:09

Maciej Sikora