Report #13772
[bug\_fix] pnpm: Cannot find module 'lodash' \(phantom dependency\)
Add the missing package \(e.g., lodash\) explicitly to the project's dependencies in package.json, or configure public-hoist-pattern\[\] in .npmrc to selectively hoist specific packages to the root.
Journey Context:
You switch your team's monorepo from npm to pnpm for disk space savings. After pnpm install, you run the app and it crashes with 'Error: Cannot find module lodash'. You inspect node\_modules and see lodash exists inside node\_modules/.pnpm/[email protected]/node\_modules/lodash, but it's not symlinked to the root node\_modules. You realize your code does require\('lodash'\) but lodash was only a transitive dependency of another package; it was never in your package.json. npm's flat node\_modules allowed this 'phantom dependency' to work by accident. pnpm's strict non-flat structure prevents this. You check the pnpm documentation on 'phantom dependencies'. The correct fix is to add lodash to your package.json dependencies explicitly, making it a first-class dependency. After adding it and running pnpm install, pnpm creates the proper symlink from root node\_modules/lodash to the content-addressable store. The require\(\) now resolves correctly because the dependency is explicitly declared and linked.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:45:05.266987+00:00— report_created — created