question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rollup - react.js does not export Component

See original GitHub issue

I’m trying to use react-redux in a project that’s bundled with Rollup. Unfortunately I’m getting repeated errors about: react/react.js does not export Component (imported by react-redux/es/components/Provider.js)

The following changes to Provider.js fix my issues (all tests passing):

diff --git a/src/components/Provider.js b/src/components/Provider.js
index db39187..030517c 100644
--- a/src/components/Provider.js
+++ b/src/components/Provider.js
@@ -1,4 +1,4 @@
-import { Component, PropTypes, Children } from 'react'
+import * as React from 'react'
 import { storeShape, subscriptionShape } from '../utils/PropTypes'
 import warning from '../utils/warning'
 
@@ -18,7 +18,7 @@ function warnAboutReceivingStore() {
   )
 }
 
-export default class Provider extends Component {
+export default class Provider extends React.Component {
   getChildContext() {
     return { store: this.store, storeSubscription: null }
   }
@@ -29,7 +29,7 @@ export default class Provider extends Component {
   }
 
   render() {
-    return Children.only(this.props.children)
+    return React.Children.only(this.props.children)
   }
 }
 
@@ -46,7 +46,7 @@ if (process.env.NODE_ENV !== 'production') {
 
 Provider.propTypes = {
   store: storeShape.isRequired,
-  children: PropTypes.element.isRequired
+  children: React.PropTypes.element.isRequired
 }
 Provider.childContextTypes = {
   store: storeShape.isRequired,

However this has the side effect of adding

var React = _interopRequireWildcard(_react);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

to both dist/react-redux.js and lib/components/Provider.js

I’m conscious of this having implications which I’m not aware of, so looking for any advice on resolving this issue, either within a Rollup plugin or otherwise.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

44reactions
blockscopedcommented, Mar 8, 2017

For anyone else following after, the solution here was to use rollup-plugin-commonjs with the following configuration:

commonjs({
    include: [
      'node_modules/**'
    ],
    exclude: [
      'node_modules/process-es6/**'
    ],
    namedExports: {
      'node_modules/react/react.js': ['Children', 'Component', 'PropTypes', 'createElement'],
      'node_modules/react-dom/index.js': ['render']
    }
  })

The namedExports section allows the plugin to correctly find the “named” exports from the React object.

See docs for more details: https://github.com/rollup/rollup-plugin-commonjs#custom-named-exports

27reactions
dousaitiscommented, Apr 2, 2018

For React v#16.2.0 the lib file name is slightly different (react.js -> index.js)

commonjs({
  include: 'node_modules/**',
  namedExports: {
    'node_modules/react/index.js': ['Component', 'PureComponent', 'Fragment', 'Children', 'createElement']
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Rollup.js can not detect default exports when trying to build ...
Rollup.js can not detect default exports when trying to build component library with React.js ; from 'rollup-plugin-babel'; ; from '@rollup/plugin ...
Read more >
Rollup.js - SVGR
SVGR provides an official rollup.js plugin to import SVG as React components. ... export if there is no other plugin handling svg files...
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
Rollup is a next-generation JavaScript module bundler. Author your app or library using ES2015 modules, then efficiently bundle them up into a single...
Read more >
How to support subpath imports using React+Rollup+Typescript
In each index.ts , export all the necessary components of the main component. Name each folder the appropriate name for the component as...
Read more >
Code-Splitting - React
Code-Splitting is a feature supported by bundlers like Webpack, Rollup and ... resolves to a module with a default export containing a React...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found