Different versions of React in an npm workspace
Asked Answered
C

0

8

We would like to have a workspace with different versions of react in two npm packages of the same workspace, due to one of them having dependencies not yet compatible with v18. However we're not able to do this without one of the versions being hoisted up to the root level, causing issues for the app in the other package.

At least I think that's what's going on. A simple reproduction goes as follows:

  • Create a new npm workspace with two packages, packages/a and packages/b; the root package.json including workspaces: ["packages/*"]
  • Create a new app in each of the workspace packages using create-react-app
  • Degrade the version in workspace a down to 17 by editing its package.json
  • Add a hook in each app, which will error if there are two versions running
  • Remove all node_modules folders, delete all package-lock.json files, and run npm i in the root folder
  • Start the two apps. This gives the "several versions of react" error with the hooks. Examining the node_modules folder in the root level we see that it contains one of the react versions
  • We also tried running npm i in each of the packages separately with the same result

In addition we tried creating a similar setup but without the full app, only installing (different versions of) react and react-dom in the two; still the root level node_modules contains one of them.

Lastly we tried using --legacy-bundling, but we haven't been able to make it work; also we're not sure it's worth the cost.

Is there a way to have a workspace with two different versions of react?

Chignon answered 21/10, 2022 at 6:50 Comment(7)
Did you file an issue in npm github issues? I'm seeing the same behavior, but google gives me nothing but this SO question...Ms
I did not, will doChignon
github.com/npm/cli/issues/6040Chignon
@Chignon it seems that the issue was closed and I believe related to this PR: github.com/npm/cli/pull/6078 So that's good news! I see as well that on the latest npm version, which for me at the moment is NodeJS v19.6.1 with npm v9.4.0, that there is an npm install --install-strategy=linked :)Horsehair
Yep, it seems it is actually also in the release notes: nodejs.org/en/blog/release/v19.6.0/#upgrade-npm-to-9-4-0: Added --install-strategy=linked option for installations similar to pnpm.Horsehair
Nice, this sounds promising!Chignon
So far it seemingly installs ok but apparently has not installed correctly (can be seen e.g. using npm ls, which has "invalid" errors). Might come back to it and investigate more laterChignon

© 2022 - 2024 — McMap. All rights reserved.