Enable full ES6 support in Node.js – dynamic transpiling with Babel

Finally I’ve decided to go beyond Promises with Node.js and started to look how can I write easily ES6 code today in my apps. I’ve had the option to use io.js which has a good ES6 support by default, starting Node.js with the Harmony flag was also an option. However if I wanted as full as possible ES6 support I had to use Babel. There are 2 options here: static compiling and deploy or dynamic compiling when the application is ran. Babel uses cache so only the changed files are recompiled.

First I had to install babel through npm:

npm install babel --save

It is recommended to install babel globally but I wanted to have it in the local packages for easy CI testing.

Then I added this little piece of code on the top of the main app.js which is the file used when the application is started with `node app.js`:

require("babel/register");

One downside is that the main (app.js) file must contain only ES5 code if the Node.js version you are using is not supporting the new syntax. However you can just create one bootstrap.js that registers babel and includes the app.js which then can contain ES6 code(and vice versa: app.js as the main one including bootstrap.js).

I wanted to have the ES6 support in the tests too and because I use grunt, mocha and istanbul I’ve added this configuration to the Gruntfile.js:

mocha_istanbul: {
    src: 'test/server/',
    options: {
        ...
        mochaOptions: ['--compilers=js:babel/register']
    }
}

That way the tests are running with transpiled files and if I have an ES6 syntax there I am good.

I also had to add/change some configuration in .jshintrc which I use in the tests and in my IDE:

“esnext” : true, //this allows ES6 syntax
“predef”: [“-Promise”] //this allows me to use a Promise lib like bluebird instead the default ES6 implementation

I also changed the JavaScript engine in my IDE (Webstorm) from EcmaScript 5.1 to ES6 to make it happy with the new syntax.

After that I started adding some fat arrow functions and the app/tests are working. It’s interesting how the new features sometimes need more than a basic change. For example the fat arrow functions behave different with this and my code for mongoose static functions that was using return this.somefunc() needed to be changed.

If you use istanbul with the configuration above you may notice that the coverage data shows ES5 code with the source map encoded at the bottom of the pages. There’s an unstable branch on istanbul’s GitHub page about source maps which means that we’ll soon have the coverage in ES6 for free! I’ll also be waiting for the Node.js and io.js to be merged and see what works without transpilers.

Converting Mongoose model functions to be Promise and callback friendly simultaneously

I just started using promises in one of my Node.js applications after watching the callback ...

Learn more

About Iliyan Trifonov

Web Developer, Blogger
Tagged , , , , . Bookmark the permalink.