Exporting Node.JS modules in Babel 6.x

Since the major updates on BabelJS on version 6 the entire transpiler requires the setup of plugins to convert code back to ES5. There’s one caveat for Node.JS modules when exporting values using the ES6 module syntax.

When you have a module with a default export, like the example below, the default value won’t be exported anymore, so you end up with a module that does not work with Node.JS as expected:

1
2
3
// example.js
export default 42;

The code above will be turned into ES5, resulting in:

1
2
3
4
5
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 42;

As you can see, the exports binding will have a default value assigned to it, but nothing else. Users of Node.JS modules would expect the default value to exported when you require it with the CommonJS syntax: var x = require('example.js');

However, that’s not the case. To fix this, use this module and you’re good to go.

This module will restore the old behaviour, adding this line back to transpiled code:

1
2
3
// ...
module.exports = exports['default'];

Fin.