module.exports vs. export default in Node.js and ES6

ES6 default exports are actually also named exports,
except that default is a "reserved" name and there is special syntax support for it. Lets have a look how Babel compiles named and default exports:

// input
export const foo = 42;
export default 21;
// output
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true

var foo = = 42;
exports.default = 21;

Here we can see that the default export becomes a property on the exports object, just like foo.

Import the module

We can import the module in two ways:
Either using CommonJS or using ES6 import syntax.

// input.js 
var bar = ...;

export default  bar;

So in order to access the default export we actually have to do:

var bar = require('./input').default;


If we use ES6 module syntax, namely:

import bar from './input';


Leave a Reply