Workspace phantom dependency / hoisting error in CI
A package imports a dependency it does not list in its own package.json. Hoisted layouts let it resolve by accident; strict pnpm node_modules expose it as a missing module.
What this error means
Code fails with "Cannot find module X" in CI (often pnpm) even though X is installed somewhere, because it is not a declared dependency of the importing package.
pnpm
Error: Cannot find module 'lodash'
Require stack:
- /workspace/packages/utils/src/index.ts
# lodash is used but not in packages/utils/package.jsonCommon causes
Undeclared dependency
The package imports something only available because another package hoisted it, not because it declared it.
Strict node_modules layout
pnpm symlinked layout does not expose undeclared packages, surfacing the missing dependency.
Relied on hoisting
A previously hoisted dependency moved, removing the accidental resolution.
How to fix it
Declare the dependency
- Add every imported package to the importing package package.json.
Terminal
pnpm --filter @acme/utils add lodashDetect phantom deps
- Scan for imports not declared as dependencies.
Terminal
npx depcheck packages/utilsHow to prevent it
- Declare every import explicitly and run depcheck in CI so phantom dependencies fail fast instead of relying on hoisting.
Related guides
Cannot find module from sibling workspace (build order) in CIFix "Cannot find module" from a sibling workspace package in CI. The dependency package was not built before…
pnpm ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL in CIFix pnpm ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL in CI. A package script failed during a recursive pnpm -r run, abo…
pnpm workspace package not found (workspace:*) in CIFix pnpm "no matching version found for workspace:*" in CI. A workspace dependency name does not match any pa…