Report #28636
[bug\_fix] Error: Cannot find module 'lodash' - phantom dependency in PNPM
Add the missing dependency explicitly to the package's package.json \(not just the monorepo root\), or configure .npmrc with public-hoist-pattern\[\]=lodash for peer dependencies that must be singletons, or use shamefully-hoist=true only as a last resort to emulate npm's flat node\_modules.
Journey Context:
You migrate a monorepo from yarn to pnpm. A package imports 'react' which is only listed in the root package.json, relying on hoisting. pnpm's strict node\_modules structure creates symlinks only to explicitly declared dependencies. The app crashes with 'Cannot find module react' despite react being present in node\_modules. You check .pnpm-debug.log and see pnpm's isolated structure. Reading pnpm.io/motivation, you understand pnpm prevents phantom dependencies. You identify missing direct dependencies via pnpm list --depth Infinity, add them to the specific package's package.json, and install. For React \(a peer dependency needing singletons\), you instead add public-hoist-pattern\[\]=react to .npmrc, allowing it to be accessible from the root while keeping other dependencies isolated.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T02:27:42.342248+00:00— report_created — created