Report #99591
[bug\_fix] Error: Cannot find module 'lodash' \(phantom dependency after switching to pnpm\)
Stop importing transitive dependencies that your package did not declare. Add the missing package directly to dependencies \(e.g., \`pnpm add lodash\`\) instead of relying on hoisting. Alternatively, use \`shamefully-hoist=true\` in .npmrc as a temporary compatibility shim, but prefer explicit dependencies.
Journey Context:
You switch a large monorepo from npm to pnpm for disk-space savings. After the switch, dozens of files throw "Cannot find module 'lodash'" even though nothing changed in source. You inspect node\_modules and notice lodash is no longer at the root; pnpm's strict node\_modules only exposes direct dependencies. You grep your source and find \`import \_ from 'lodash'\` in a package whose package.json never listed lodash—it only worked before because npm's flat hoisting happened to place lodash at the root. You add lodash to that package's dependencies, reinstall, and the phantom-dependency errors disappear across the workspace.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:43:46.507476+00:00— report_created — created