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).
You will first need:
- babel-cli: Used to compile files from the command line
- babel-core: Core API
(Babel in itself does not do anything and it is the plugins that will define what it does.)
$ npm install --save-dev babel-cli babel-core babel-preset-es2015
The standard way of configuring Babel is to use a .babelrc file:
.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
Whichever way you choose, inside your configuration put:
index.js file with the following ES2015 code:
var numbers = [1, 4, 9];
Run it with
node index.js and check that everything is working.
When using certain features of ES6, you might encounter an error of type:
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:
Start your app with
This will clear the errors.