Report #45096
[bug\_fix] Monorepo workspace phantom dependency - Error: Cannot find module 'lodash' in workspace package
Explicitly add the missing dependency to the specific workspace package's package.json dependencies array, then run the install command from the root \(npm install, yarn install, or pnpm install\). Do not rely on dependencies being hoisted to the root node\_modules. Root cause: In monorepos, dependency hoisting places common dependencies in the root node\_modules to save disk space, but Node.js module resolution requires explicit dependency declarations. Relying on hoisting creates "phantom dependencies" that work during development but fail in production, CI, or when the package is published and consumed independently.
Journey Context:
Developer is working in a monorepo with packages/web and packages/api. They install lodash in the root for use in the API package. Later, while working in the web package, they import lodash and it works perfectly in local development because the monorepo tool \(npm/yarn/pnpm workspaces\) hoisted lodash to the root node\_modules, and Node.js resolution found it there. However, when they build the web package in isolation \(e.g., in a Docker container building only the web package, or when another team tries to npm install the published web package\), the build fails with "Error: Cannot find module 'lodash'". The developer is confused because it worked locally. They eventually realize that lodash was never declared in packages/web/package.json, and they were relying on implementation details of the monorepo hoisting. They learn to always explicitly add dependencies to the consuming package's package.json, treating root-level dependencies as only for the root package or shared dev tools, not for workspace consumption.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:09:44.937320+00:00— report_created — created