EasyMonorepo📖 Theory Question

What are pnpm workspaces and why are they preferred in monorepos?

💡

Hint

pnpm hoists deps once via hard links — saves disk space and prevents phantom dependencies vs npm/yarn workspaces

Full Answer

pnpm workspaces link all packages in a monorepo together so they can import each other as if they were published packages, while sharing a single node_modules store.

# pnpm-workspace.yaml
packages:
  - 'apps/*'
  - 'packages/*'

Why pnpm over npm/yarn workspaces:

1. Hard-link store — pnpm stores each package version once globally and hard-links into projects. A 100-package monorepo doesn't duplicate React 100 times on disk.

2. Strict dependency resolution — pnpm only allows importing packages listed in a package's own package.json. npm/yarn hoist everything, enabling "phantom dependencies" (importing a transitive dep that isn't declared — breaks when the transitive dep is removed).

3. Speed — hard links are faster to set up than copies; install times drop 2–5× vs npm on large repos.

4. Filteringpnpm --filter @org/ui build runs a command in just one package; pnpm -r build runs it across all.

More Monorepo Questions

EasyWhat is a monorepo and what are its advantages over a polyrepo setup?EasyHow does Turborepo's caching work and what makes it fast?EasyHow does Nx differ from Turborepo?EasyWhat are module boundary rules in Nx and why do they matter?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint