205

Before, babel would add the line module.exports = exports["default"]. It no longer does this. What this means is before I could do:

var foo = require('./foo');
// use foo

Now I have to do this:

var foo = require('./foo').default;
// use foo

Not a huge deal (and I'm guessing this is what it should have been all along). The issue is that I have a lot of code that depended on the way that things used to work (I can convert most of it to ES6 imports, but not all of it). Can anyone give me tips on how to make the old way work without having to go through my project and fix this (or even some instruction on how to write a codemod to do this would be pretty slick).

Thanks!

Example:

Input:

const foo = {}
export default foo

Output with Babel 5

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var foo = {};
exports["default"] = foo;
module.exports = exports["default"];

Output with Babel 6 (and es2015 plugin):

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var foo = {};
exports["default"] = foo;

Notice that the only difference in the output is the module.exports = exports["default"].


Edit

You may be interested in this blogpost I wrote after solving my specific issue: Misunderstanding ES6 Modules, Upgrading Babel, Tears, and a Solution

5
  • I'm curious, what are the cases where need require if you are working in a codebase that uses Babel? Chances are, there are other approaches that would allow you to avoid that anyway. Nov 3, 2015 at 18:10
  • I'm leveraging a feature of Webpack which will not require code if it's found in dead code like: if (false) { require('./foo') } with webpack would skip actually including foo.js in the resulting bundle.
    – kentcdodds
    Nov 3, 2015 at 18:11
  • What ends up being your false toggle there? If it's a condition that is available in your webpack config, there may be another option. Nov 3, 2015 at 18:19
  • 1
    This one caused me issues for hours before I found this post. I ended up replacing all of my export default {foo, bar} with module.exports = {foo, bar}. I quite liked the incorrect method which is now not supported.
    – stumct
    Jan 23, 2016 at 16:40
  • @loganfsmyth It's very useful for passing around entire modules without much repetition in code. Have a look at this gist gist.github.com/loopmode/3eeaf0764c30439add1d8008e39d0267 May 7, 2018 at 20:02

4 Answers 4

109

If you want CommonJS export behavior, you'll need to use CommonJS directly (or use the plugin in the other answer). This behavior was removed because it caused confusion and lead to invalid ES6 semantics, which some people had relied on e.g.

export default {
  a: 'foo'
};

and then

import {a} from './foo';

which is invalid ES6 but worked because of the CommonJS interoperability behavior you are describing. Unfortunately supporting both cases isn't possible, and allowing people to write invalid ES6 is a worse issue than making you do .default.

The other issue was that it was unexpected for users if they added a named export in the future, for example

export default 4;

then

require('./mod');
// 4

but

export default 4;
export var foo = 5;

then

require('./mod')
// {'default': 4, foo: 5}
8
  • I agree with you (and noted) that the before behavior was incorrect, but my question was how to get around the issue. I was relying heavily on the incorrect behavior (didn't realize it was incorrect until this morning). I would prefer to not have to update everything all at once...
    – kentcdodds
    Nov 3, 2015 at 18:10
  • The only fix to get the current behavior would to be switch your code to use CommonJS directly, or to stay on Babel 5 until you have time to update. Nov 3, 2015 at 18:12
  • 4
    @kentcdodds we can write a webpack loader to keep this working (or a babel plugin). I'm surprised they're not providing one (or publicizing the change more heavily!) Nov 3, 2015 at 18:20
  • I am confused by this... if I do export default function () {} in module A and then import a from 'a' in module B, with Babel 6 a would be { default: function () {} }... From what I can understand from exploringjs.com/es6/… this should work and I should get the exported function in B, not the object.
    – mamapitufo
    Jan 18, 2016 at 16:49
  • @mamapitufo That should work, but it's hard to say what's wrong without an example to look at. Feel free to drop by Babel's support channel on Slack if you want to chat. Jan 18, 2016 at 17:07
94

You can also use this plugin to get the old export behavior back.

5
  • 1
    I knew someone would write a plugin for sooner or later. Thanks!
    – kentcdodds
    Nov 16, 2015 at 13:33
  • sadly babel-plugin-add-module-exports doesn't support amd-style modules (yet)
    – zowers
    Nov 23, 2015 at 9:54
  • 3
    I've used the babel-plugin-transform-es2015-modules-simple-amd to solve this same issue in my project that has AMD modules
    – Tom Wayson
    Feb 20, 2016 at 19:52
  • I think using UMD and this plugin is the way to go! Thanks Apr 21, 2017 at 6:14
  • Very very helpful. May 7, 2018 at 19:56
34

For library authors you may be able to work around this problem.

I usually have an entry point, index.js, which is the file I point to from the main field in package.json. It does nothing other than re-export the actual entry point of the lib:

export { default } from "./components/MyComponent";

To workaround the babel issue, I changed this to an import statement and then assign the default to module.exports:

import MyComponent from "./components/MyComponent";
module.exports = MyComponent;

All my other files stay as pure ES6 modules, with no workarounds. So only the entry point needs a change slightly :)

This will work for commonjs requires, and also for ES6 imports because babel doesn't seem to have dropped the reverse interop (commonjs -> es6). Babel injects the following function to patch up commonjs:

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 

I've spent hours battling this, so I hope this saves someone else the effort!

2
  • For some reason, I never got my heads spinned right about module.exports and export default stuff. Now we're back to square one?
    – windmaomao
    Feb 20, 2018 at 17:44
  • @windmaomao what do you mean? This is a trick so that commonjs users do not have to require("whatever").default. If you're not a library author, this is probably irrelevant Feb 21, 2018 at 15:37
2

I have had such kind of issue. And this is my solution:

//src/arithmetic.js

export var operations = {
  add: function (a, b) {
      return a + b;
  },

  subtract: function (a, b) {
      return a - b;
  }
};

//src/main.js

import { operations }  from './arithmetic';

let result = operations.add(1, 1);

console.log(result);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.