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-cli: Used to compile files from the command line
- babel-core: Core API
- babel-preset-es2015: Collection of all the plugins needed to write ES2015 JavaScript.
(Babel in itself does not do anything and it is the plugins that will define what it does.)
Installation
$ 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:
{ |
Testing
Create an 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.
Create a launcher
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 node launcher.js
This will clear the errors.