This article gives you a basic way of setting up your environment if you want to use ES2015 with Node (using v4.3.0 in this tutorial).

Babel libraries

You will first need:

(Babel in itself does not do anything and it is the plugins that will define what it does.)

Installation

1
$ npm install --save-dev babel-cli babel-core babel-preset-es2015

Babel configuration

The standard way of configuring Babel is to use a .babelrc file:

Put a .babelrc configuration file at the root of your project. I don’t like to have too much stuff in there and thankfully you can also put your config in package.json.

Whichever way you choose, inside your configuration put:

1
2
3
{
"presets": ["es2015"]
}

Testing

Create an index.js file with the following ES2015 code:

1
2
3
var numbers = [1, 4, 9];
var doubles = numbers.map(num => num * 2);
console.log(doubles); // [2, 8, 18 ]

Run it with node index.js and check that everything is working.

Create a launcher

When using certain features of ES6, you might encounter an error of type:

1
SyntaxError: Unexpected token

This means you will need to create a launcher for your app that loads 'babel-core/register' ahead of your code.

Assuming your entry point is index.js, create the following launcher:

1
2
3
4
5
// -----------
// launcher.js
// -----------
require('babel-core/register');
require('./index');

Start your app with node launcher.js

This will clear the errors.