Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

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

Comments

@lxe
Copy link
Contributor

lxe commented Sep 20, 2016

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
Copy link

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.

@danielkcz
Copy link

danielkcz commented Sep 30, 2016

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.

@Rich-Harris
Copy link
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
Copy link

NamedExports doesn't work in all cases.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants