Skip to content
Latchkey

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.json

Common 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

  1. Add every imported package to the importing package package.json.
Terminal
pnpm --filter @acme/utils add lodash

Detect phantom deps

  1. Scan for imports not declared as dependencies.
Terminal
npx depcheck packages/utils

How to prevent it

  • Declare every import explicitly and run depcheck in CI so phantom dependencies fail fast instead of relying on hoisting.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →