Today ES6 specification known as ES2015 is almost supported in many JavaScript engines but not completely. For programmers sometimes is very frustranting cannot use some interesting and useful new language features.

In the case of ES6 JavaScript developer that want use new ES6 functionalities scuh us arrow function, class, generators, promise, maps, sets now can do that thanks to tools as babel and webpack.

In this article i explain how to setup ES6 to use it in the Node.js environment. To do that we transpile ES6 code dynamically (at runtime) thanks to Babel.

In general there are two alternatives to transpile ES6 code, static and dynamic. Dynamic approach is convenient for experiments and development because it uses on the fly transpilation, which means that startup is slower and Babel need to be installed on the production system. We focus on the dynamic approch and we’ll configure a Node.js project to use it.

The first step is to create an empty Node.js project with your favourite IDE or simply make an empty folder and create a package.json file like this:

In the previous module we just created and exported a class Person with properties _firstName and _lastName and their getter, setter methods with additional toString method that have the responsability to print the properties’ values.

To complete our project we have to create a file named myapp.js that will be the application starting point as reported below:

It’s time to start our first ES6 application in Node.js environment so execute the following command:

1

npm start

npm start

The output will be something like that:

1
2
3
4
5
6
7

&gt; MyApplication@0.0.1 start G:\Projects\Web\GitHub\BNode-ES6
&gt; babel-node ./myapp.js
You are Brendan Eich
You are Ada Lovelace
You are Dennis Ritchie
You are James Gosling
G:\Projects\Web\GitHub\BNode-ES6&gt;

&gt; [email protected] start G:\Projects\Web\GitHub\BNode-ES6
&gt; babel-node ./myapp.js
You are Brendan Eich
You are Ada Lovelace
You are Dennis Ritchie
You are James Gosling
G:\Projects\Web\GitHub\BNode-ES6&gt;

Now you are ready to start use all ES6 features in your Node.js project.

At pack.ly (https://www.pack.ly/create) we use it because more features are very useful for us and especially because they make the javascript code more understandable at semantic level.

You can find the scaffolding of the code described in this article here:

In this project i used Node.js 4.3.1 so in package.json i installed “babel-preset-es2015-node4” and in babel configurations under presets keys i setted “es2015-node4”. If you have different version or configuration please refer to Babel docume ntation here: https://babeljs.io/