Report #5393
[bug\_fix] Error: Cannot find module 'lodash' \(MODULE\_NOT\_FOUND\) despite transitive dependency existing in node\_modules/.pnpm
Add the missing package explicitly to package.json dependencies, or use .pnpmfile.cjs to inject peer dependencies. Alternatively, set shamefully-hoist=true in .npmrc to temporarily flatten node\_modules. Root cause: pnpm uses a strict isolated node\_modules structure where packages can only access explicitly declared dependencies; phantom dependencies \(using transitive deps not listed in package.json\) that work in npm/yarn fail in pnpm.
Journey Context:
Developer switches a project from npm to pnpm for disk space savings. After pnpm install, the app crashes with MODULE\_NOT\_FOUND for lodash. Checking node\_modules reveals lodash exists in node\_modules/.pnpm/lodash@version/node\_modules/lodash. However, the code tries to require\('lodash'\) in a file where lodash isn't a direct dependency. In npm/yarn, this worked because of hoisting making transitive dependencies available at root. pnpm's strict isolation prevents this phantom dependency pattern. The developer can add lodash explicitly to package.json, or if migrating a large legacy codebase, temporarily enable shamefully-hoist in .npmrc to mimic npm's flat structure while fixing declarations. The root cause is pnpm prevents accidental reliance on undeclared transitive dependencies.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:11:58.842541+00:00— report_created — created