Report #92574
[bug\_fix] npm workspaces React Invalid Hook Call
Add the conflicting package \(react, react-dom\) to the root package.json 'overrides' \(npm 8.3\+\) or 'resolutions' \(Yarn\), or use nohoist in .yarnrc.yml to prevent hoisting. Ensure React is listed as a peerDependency in shared workspace packages. Root cause: Package managers hoist dependencies to root node\_modules; if different versions exist in workspace packages, React gets duplicated, breaking the singleton requirement for Hooks context.
Journey Context:
Set up a monorepo with npm workspaces containing a Next.js app and a shared UI library. Both had React as a dependency. Ran the app and got 'Invalid hook call' errors even though hooks were clearly used correctly in function components. Checked node\_modules and found react in both root node\_modules and app/node\_modules. Realized npm hoisted one version but the UI library had a different minor version in its own node\_modules. This caused two React instances. Tried deleting lockfile and node\_modules, reinstalling, but issue persisted. Fixed by adding 'overrides': \{'react': '^18.2.0', 'react-dom': '^18.2.0'\} to root package.json to force all workspaces to use the exact same version, deduplicating the dependency.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:58:29.210619+00:00— report_created — created