Free email newsletter: “ News


Using the ES6 transpiler Babel on Node.js

This blog post is outdated (it covers Babel 5). Please read Sect. “Node.js setup: Dynamically transpiled ES6 via Babel” in “Setting up ES6”.

This blog post explains how to use the ES6 transpiler Babel with Node.js. You can download the code shown in this post on GitHub. For further information on ECMAScript 6, consult the ebook “Exploring ES6”.

Warning: The approach explained in this post is convenient for experiments and development. But it uses on-the-fly transpilation, which may be too slow for your production code. Then you can transpile as a build step (as explained in the Babel documentation).

Running normal Node.js code via Babel

The npm package babel brings Babel support to Node.js:

    $ npm install --global babel

This package contains the shell script babel-node, which is a Babel-ified version of node. It compiles everything from ES6 to ES5 that is run or required. For example, you can start a REPL via the following shell command:

    $ babel-node

In the REPL, you can use ES6:

    > [1,2,3].map(x => x * x)
    [ 1, 4, 9 ]

babel-node also lets you run Node.js scripts such as the following one.

    // point.js
    export class Point {
        constructor(x, y) {
            this.x = x;
            this.y = y;
    if (require.main === module) {
        let pt = new Point(7,4);
        console.log(`My point: ${JSON.stringify(pt)}`);

The following shell command runs point.js:

    $ babel-node point.js
    My point: {"x":7,"y":4}

The package babel has many more features, which are all documented on the Babel website. For example, from within a normal Node module, you can install a “require hook”, which compiles all required modules via Babel (except, by default, modules in node_modules).

Running Jasmine unit tests via Babel

Another npm package, babel-jest, is a preprocessor for the Jasmine-based unit testing tool Jest.

One way to install babel-jest is by mentioning it in the devDependencies of your package.json:

      "devDependencies": {
        "babel-jest": "*",
        "jest-cli": "*"
      "scripts": {
        "test": "jest"
      "jest": {
        "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
        "testFileExtensions": ["js"],
        "moduleFileExtensions": ["js", "json"],
        "testDirectoryName": "spec"

Afterwards, you only need to execute the following command inside the directory of package.json and both babel-jest and a command line interface (CLI) for Jest will be installed.

    npm install

The configuration options for Jest are documented on its website. I have used testDirectoryName to specify that the tests are inside the directory spec (the default is __tests__). Let’s add the following test file to that directory:

    // spec/point.spec.js
    import { Point } from '../point';
    describe('Point', () => {
        it('sets up instance properties correctly', () => {
            let p = new Point(1, 5);

Because we have specified scripts.test in package.json, we can run all tests inside spec/ via the following command:

    npm test


Axel Rauschmayer said...

Thank you, good information. I didn't know you can transpilate ES6 code on-the-fly by using babel-node. Maybe you could compare the pros & cons of Babel vs Traceur

Axel Rauschmayer said...

Differences between Babel and other ES6 transpilers (available only in cache...)

Axel Rauschmayer said...

Traceur is dead, use Babel ;)
Now seriously, Babel has more features + supports JSX out of the box (useful if you're using React). It's also going to be included in Sprockets 4 (i.e. automatically available in Rails). I think at this point it's clear Babel is getting more traction, and even Google moved to it in some projects.

Axel Rauschmayer said...

Babel is slower than Traceur. Babel couldn't compile my project's source code, I had to Ctrl+C it. But I do like babel-node to test small es6 stuff.

Axel Rauschmayer said...

Is your code broken up into node-style modules? You can have Bable run on a per-file basis, then use something like browserify to build your browser versions.

Axel Rauschmayer said...

Here's how I use babel with mocha, eslint and coverage reporting...

Axel Rauschmayer said...

cool, hope JSX will be killed really soon, stop breaking JS syntax :)

Axel Rauschmayer said...

What version of node did you use? According to an open issue in jest, it's not yet compatible with v0.12.x and furthermore node needs to execute with the --harmony flag. Did you face this issue?

Axel Rauschmayer said...

Everything worked nicely. Maybe because jest was never faced with ES6 code, only with the transpilation result, ES5 code?

Axel Rauschmayer said...

I was trying to use the grunt-jest plugin to run the tests through grunt and that was going through jest's JasmineTestRunner.js which requires v0.12 or the harmony flag. The jest-cli bin requires the harmonize module, so that works fine with node < 0.12. On a side-note: Jest depends on jsdom and if you check the latest release of jsdom, it references nodejs as legacy technology :-)

Axel Rauschmayer said...

It's been a bliss to write my tests in ES6 using Mocha and Babel:

'scripts': {
'test': 'mocha --compilers js:babel/register'

Axel Rauschmayer said...

How did you go about using babel-node on the back-end in production?

Axel Rauschmayer said...

I haven’t yet, but if I did, I’d compile an ES6 directory to an ES5 directory, probably via gulp.

Axel Rauschmayer said...

Interesting. so you wouldn't try to run it with the hook or via babel-node? I'm trying to figure what the best solution is as all of them have some issues.