pnpm monorepo zod dependency-resolution workspace

pnpm Strict Dependency Resolution in Monorepo

pnpm Strict Dependency Resolution in Monorepo

Problem

Build failed with:

Cannot find module 'zod' imported from '/path/packages/ui/src/schemas.ts'

Even though zod was installed in apps/zendee-adventures/package.json, the shared packages/ui package couldn’t resolve it.

Root Cause

pnpm uses strict dependency isolation by default (unlike npm/yarn which hoist everything to the root node_modules/). Each workspace package can only access dependencies explicitly declared in its own package.json.

packages/ui/src/schemas.ts imported zod, but zod was only listed as a dependency of apps/zendee-adventures, not packages/ui.

Solution

Add the dependency to the package that actually imports it:

// packages/ui/package.json
{
  "dependencies": {
    "zod": "^3"
  }
}

Then run pnpm install to update the lockfile.

Prevention

  • Rule: If a file in packages/X imports a module, that module must be in packages/X/package.json — not just in a consuming app.
  • This is actually a feature of pnpm — it catches “phantom dependencies” that npm/yarn silently allow through hoisting.
  • When creating shared packages, audit all imports and ensure each one is declared as a dependency.