Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollup Error: 'isValidElementType' is not exported by node_modules/react-is/index.js

I'm building a bundle with rollUp using styled-components.

My rollup.config.js looks like:

import resolve from 'rollup-plugin-node-resolve'
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
export default {
  input: 'src/index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'cjs'
  },
  external: [
    'react',
    'react-proptypes'
  ],
  plugins: [
    resolve({
      extensions: [ '.js', '.json', '.jsx' ]
    }),
    commonjs({
      include: 'node_modules/**'
    }),
    babel({
      exclude: 'node_modules/**'
    })
  ]
}

And I'm receiving

[!] Error: 'isValidElementType' is not exported by node_modules/react-is/index.js
https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
node_modules/styled-components/dist/styled-components.es.js (7:9)
5: import stream from 'stream';
6: import PropTypes from 'prop-types';
7: import { isValidElementType } from 'react-is';
            ^
8: import hoistStatics from 'hoist-non-react-statics';

Checking on the node_modules itself react-is is a commonjs module as it can be checked out here as well.

Shouldn't the commonjs plugin take care of it since it is inside node_modules/** ?

Thanks.

like image 703
FabioCosta Avatar asked Apr 28 '18 20:04

FabioCosta


3 Answers

I resolved this by using the rollup-plugin-commonjs

and defining the export manually in the rollup config as below

export default {
  input: 'src/index.js',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      sourcemap: true
    },
    {
      file: pkg.module,
      format: 'es',
      sourcemap: true
    }
  ],
  plugins: [
    external(),
    postcss({
      modules: true
    }),
    url(),
    babel({
      exclude: 'node_modules/**'
    }),
    resolve(),
    commonjs({
      include: 'node_modules/**',
      namedExports: {
        'node_modules/react-is/index.js': ['isValidElementType']
      }
    })
  ]
}

After this everything worked fine.

and for the info, my initial setup was done through https://github.com/transitive-bullshit/react-modern-library-boilerplate

Hope it works for you

like image 55
Martin Ratinaud Avatar answered Oct 24 '22 06:10

Martin Ratinaud


The fix above didn't work for me. However, adding styled-components to the rollup.config.js list of externals and globals worked for me.

    export default {
      ...
      external: ['styled-components'],
      ...
      globals: { 'styled-components': 'styled' },
    };

I'm using the typescript create-react-library cli, which comes bundled with rollup.

https://github.com/styled-components/styled-components/issues/930

like image 17
FrostyDog Avatar answered Oct 24 '22 06:10

FrostyDog


The solution posted by FrostyDog worked for me so +1 for that, HOWEVER...

I immediately got the same error when trying to import hooks from react, I could follow the above solution and do

    export default {
      ...
      external: ['styled-components', 'react'],
      ...
    };

in rollup.config.js but I wanted a solution that handled this for all libraries, so this wasn't a recurring issue in the future.

So did this instead...

import external from "rollup-plugin-peer-deps-external";
...
export default {
    ...
    plugins: [
        external({
            includeDependencies: true
        }),
        ...
    ]
};

solved it for me and everyone adding to the project after.

like image 4
Master Noob Avatar answered Oct 24 '22 06:10

Master Noob