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.

“Duplicate React” is an incredibly common issue that produces weird errors and confuses beginners and advanced users alike. It happens when you install react but some component you’re using installs its own copy of react (often with a different version).

React doesn’t throw in such cases because there are situations in which multiple React may validly coexist in the page (e.g. a third-party React-powered widget on a React-powered website). However it seems to make much less sense inside a single bundle. Webpack is in a perfect position to do this, but of course it won’t add any library-specific code. The good news is that we’re on top of webpack, so we might be able to do something.

There are several possible options:

    1. Do nothing. (Like now)
    1. Warn/error on duplicate React in the bundle.
    1. Alias React to always be the one on the top level, regardless of Node resolution mechanism.

Option (3) seems like the easiest to do (we can special-case React in our Webpack, and maybe Jest, configs). It also “just works” from the user’s point of view because they don’t need to delete any node_modules or mess with projects’ peerDependencies. The downside is the ecosystem loses because project authors don’t become aware that their components incorrectly depend on React, or that their components are outdated.

Option (2) seems like more work. It’s not immediately clear what message we could provide. Would we tell the user to delete the extra react from the respective node_modules subfolder? Would we tell them to file an issue with that component or library?

Finally, we could stick with doing nothing. Any thoughts?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
gaelolliviercommented, Sep 19, 2016

I think this doesn’t only apply to React. Already had some troubles with duplicate dependencies in my bundle and I would love the bundler to tell me when that happens / allow me to prevent it. This is especially a problem when using npm link to link multiple private dependencies

3reactions
thangngoc89commented, Sep 18, 2016

@gaearon I can do it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate ReactJS import issue when using npm link to test ...
It seems you have 2 reacts, works when you publish package to NPM and then import it directly from NPM. but doesn't work...
Read more >
Invalid Hook Call Warning - React
This works because custom Hooks are also supposed to only be called while a function component is rendering. Duplicate React. In order for...
Read more >
Duplicate Copy of React Errors When Using NPM Link - Medium
Duplicate Copy of React Errors When Using NPM Link. Our team has been hard at work developing shared component and shared state libraries ......
Read more >
Using the React.cloneElement() function to clone elements
In this in-depth tutorial, learn how to clone and manipulate elements three different ways using the React.cloneElement function.
Read more >
Duplicate Packages Found: How to resolve React hoisting into ...
Step 1: Confirm package versions are correct · Step 2: Confirm package resolutions are correct · Step 3: Remove the duplicate packages and ......
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