Using the ES6 transpiler Babel on Node.js

[2015-03-15] esnext, dev, javascript
(Ad, please don’t block)

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
jest.autoMockOff();
import { Point } from '../point';

describe('Point', () => {
    it('sets up instance properties correctly', () => {
        let p = new Point(1, 5);
        console.log(JSON.stringify(p));
        expect(p.x).toBe(1);
        expect(p.y).toBe(5);
    });
});

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

npm test