module.exports = { foo: 'bar' } fails to create named exports and results in "does not export foo" error . #115

Closed
lxe opened this Issue Sep 20, 2016 · 4 comments

Projects

None yet

5 participants

@lxe
Contributor
lxe commented Sep 20, 2016 edited

Consider the following commonjs module:

// mycjsmodule.js
module.exports = {
   foo: 'bar'
};

If I attempt to do...

import { foo } from './mycjsmodule';
console.log(foo);

...rollup will produce the following error:

Module /mycjsmodule.js does not export foo (imported by /entrypoint.js)

Looks like the module gets transpiled into:

var mycjsmodule = {
  foo: 'bar'
}

export default mycjsmodule;
export { mycjsmodule as __moduleExports };

However, if I do module.exports.foo = 'bar', this error does not occur. The transpiled code is:

var foo = 'bar';

var mycjsmodule = {
    foo: foo
};

export default mycjsmodule;
export { mycjsmodule as __moduleExports };
export { foo };

Many commonjs exports utilize the module.exports = { ... } pattern, and each property in the module.exports object should be properly transpiled to a named export.


This specifically affects the React npm module which exports all of its named exports as properties on an object that gets later assigned to modules.exports:

var React = {
...
 Component: ReactComponent,
...
}

module.exports = React;

This makes the following common code fail:

import React, {Component} from 'react';
@timdp
timdp commented Sep 20, 2016

With version 5.0.4, I'm seeing the same issue with rx.disposables. It doesn't occur anymore after downgrading to 4.1.0.

@FredyC
FredyC commented Sep 30, 2016 edited

Same issue with redux-tcomb

module.exports = {
  t: require('tcomb'),
  createCheckedMiddleware: require('./lib/createCheckedMiddleware'),
  createCheckedReducer: require('./lib/createCheckedReducer'),
  createActionType: require('./lib/createActionType')
};

Error: Module D:\workspace\behavior3-chief\node_modules\redux-tcomb\index.js does not export createCheckedReducer (imported by D:\workspace\behavior3-chief\src\core\ReduxStore.js)

I also do confirm that downgrading to 4.1.0 solved problem. Seems that optimization introduced in #106 caused this problem.

@pskhodad pskhodad referenced this issue in driftyco/ionic-app-scripts Oct 2, 2016
Closed

Using CommonJS modules with rollup #16

@ckaeslin ckaeslin added a commit to ckaeslin/rollup-plugin-commonjs that referenced this issue Oct 4, 2016
@ckaeslin ckaeslin This is a fix for rollup#115 and driftyco/ionic-app-scripts#16 2687b54
@Rich-Harris
Contributor

Apologies for the delay. More recent versions of Rollup will give you a more helpful error message pointing to this page: https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module

Essentially, you need to use the namedExports option documented on the README.

@DanielJoyce

NamedExports doesn't work in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment