Skip to content

Instantly share code, notes, and snippets.

@tannerlinsley
Last active February 19, 2022 09:45
Show Gist options
  • Save tannerlinsley/4778cf30d66530ea9802899168119ec0 to your computer and use it in GitHub Desktop.
Save tannerlinsley/4778cf30d66530ea9802899168119ec0 to your computer and use it in GitHub Desktop.
How to fix the multiple-instances of React error when using linked NPM modules that import React as a peer dependency

How to fix the multiple-instances of React error when using linked NPM modules that import React as a peer dependency

Back-link your linked module's React dependency back to your application's React

In your app that you are receiving the multiple-version error:

  • Go to node_modules/react
  • Run yarn link

In your linked module that also uses React:

  • Run yarn link react

This will result in both React instances resolving to the same file (your app's node_modules/react import)

Use Rescripts (my favorite)

Rescripts is great. Because you can do this:

  • yarn add @rescripts/cli
  • Replace your calls to react-scripts with rescript calls
  • Add this file to the root of your project:

.rescriptsrc.js:

const path = require('path')
const resolveFrom = require('resolve-from')

const fixLinkedDependencies = config => {
  config.resolve = {
    ...config.resolve,
    alias: {
      ...config.resolve.alias,
      react$: resolveFrom(path.resolve('node_modules'), 'react'),
      'react-dom$': resolveFrom(path.resolve('node_modules'), 'react-dom'),
    },
  }
  return config
}

module.exports = [
  fixLinkedDependencies,
]
  • Run yarn start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment