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

[!] Error: 'default' is not exported by ../../node_modules/prop-types/index.js #361

Closed
samonxian opened this issue Dec 7, 2018 · 20 comments

Comments

@samonxian
Copy link

samonxian commented Dec 7, 2018

rollup config

// rollup.config.js
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'umd',
    name: umdName,
    indent: false,
    sourcemap: false,
  },
  plugins: [
    nodeResolve(),
    commonjs({
      include: 'node_modules/**', // Default: undefined
    }),
  ],
};

When running rollup -c,it will throw the error.
I have been looking for this problem for a while, but there is no solution.
But when using commonjs without any options,it wok fine.

// rollup.config.js
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'umd',
    name: umdName,
    indent: false,
    sourcemap: false,
  },
  plugins: [
    nodeResolve(),
    commonjs(),
  ],
};
@TrySound
Copy link
Member

TrySound commented Dec 7, 2018

@xianshannan Do you have monorepo?

@TrySound
Copy link
Member

TrySound commented Dec 7, 2018

Try /node_modules/ regexp

@samonxian
Copy link
Author

samonxian commented Dec 8, 2018

@TrySound I have monorepo.
I had try /node_modules/,but namedExports did not work.
It throws the error:
[!] Error: 'isValidElementType' is not exported by ../../node_modules/react-is/index.js

// rollup.config.js
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'umd',
    name: 'umdName',
    indent: false,
    sourcemap: false,
  },
  plugins: [
    nodeResolve(),
    commonjs({
      namedExports: {
        'node_modules/react-js/index.js': ['isValidElementType'],
      },
      include: [
        /node_modules\/prop-types/,
        /node_modules\/hoist-non-react-statics/,
        /node_modules\/invariant/,
        /node_modules\/react-is/,
        /node_modules\/warning/,
      ],
    }),
  ],
};

@TrySound
Copy link
Member

TrySound commented Dec 8, 2018

I use names which I import from

      namedExports: {
        'react-js': ['isValidElementType'],
      },

@samonxian
Copy link
Author

@TrySound Thanks, it did work.

@NathanielHill
Copy link
Contributor

@TrySound Thanks for this! Finally realized my node modules being ../../ down from rollup's cwd was causing most of my problems!

@NathanielHill
Copy link
Contributor

I wonder if there should be a note in the docs about monorepos? Most tools play nice with different node_modules location

@josefaidt
Copy link

@NathanielHill I'm having this exact same issue with a monorepo. Came to rollup for a react component library and a note like that would be incredibly helpful.

@NathanielHill
Copy link
Contributor

@josefaidt Did you fix your problem?

@NathanielHill
Copy link
Contributor

I created a pull request to update the docs with a note: #372

@josefaidt
Copy link

@NathanielHill No I have not. Using the include: /node_modules/ property did not work unfortunately.

@NathanielHill
Copy link
Contributor

@josefaidt You also need to be careful about whether /node_modules/ is transpiled by babel or not. You can also use regexes there and I would advise that as well.

@josefaidt
Copy link

josefaidt commented Feb 12, 2019

Ah that did it. Didn't think about the transpilation at first. I had to also add all of my dependencies to externals as shown below:

export default {
  input: './src/index.js',
  output: {
    file: './build/bundle.js',
    name: 'SomeModule',
    format: 'iife',
    sourcemap: 'inline',
  },
  plugins: [
    babel({
      plugins: babelPlugins,
      presets: babelPresets,
      exclude: /node_modules/,
    }),
    url(),
    postCss({ modules: true }),
    nodeResolve(),
    cjs({ include: /node_modules/ }),
  ],
  external: ['react', 'react-dom', 'prop-types', 'styled-components'],
}

@NathanielHill
Copy link
Contributor

🎉 Exactly! There's a plugin to generate the externals automatically from package.json, but it didn't work for me at all, and apparently it doesn't support namespaced packages.

Would be nice if that worked.

@josefaidt
Copy link

Yes, thank you for your timely assistance! Thankfully this will be a relatively small library in terms of packages used, but I agree it would be nice to do it automatically.

@sunyongjian
Copy link

+1

namedExports always is not work, I have tried all the above methods.

@zhaowenjian
Copy link

+1 , is this a bug ? the rollup-plugin-commonjs doc about namedExports not useful?

@bwhitty
Copy link

bwhitty commented Aug 8, 2019

To go with #361 (comment) and anyone else reading this (and maybe working in a monorepo), to fix prop-types fully, this works:

commonjs({
  // https://github.com/rollup/rollup-plugin-commonjs#usage-in-monorepo
  include: /node_modules/,
  namedExports: {
    // node_modules/prop-types/factoryWithTypeCheckers.js#L115
    'prop-types': [
      'array',
      'bool',
      'func',
      'number',
      'object',
      'string',
      'symbol',
      'any',
      'arrayOf',
      'element',
      'elementType',
      'instanceOf',
      'node',
      'objectOf',
      'oneOf',
      'oneOfType',
      'shape',
      'exact',
    ],
  },
}),

Alternatively, and against the suggestions of prop-types's, documentation, marking prop-types as a peerDependency (assuming you're bundling a package) and using rollup-plugin-peer-deps-external would work as well.

@AntonioRedondo
Copy link

I've been having an absolute inconsistent behaviour with the commonjs plugin and the include property. I had to play with these three values in order to Rollup to compile:

commonjs({
    include: [
        "node_modules",
        "node_modules/**",
        "node_modules/**/*",
    namedExports: { ... },
}),

Don't put all three, just use the one that makes namedExports working, mostly node_modules/** and node_modules/**/*.

@TrySound
Copy link
Member

TrySound commented Oct 3, 2019

@AntonioRedondo RegEx is more universal here

commonjs({
  include: /node_modules/,
  namedExports: { ... }
})

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

8 participants