How-To Guide

How to Set Up pnpm Workspaces

Manage multiple packages in one repository with linked dependencies, a single lockfile and reproducible installs.

On this page

  1. Introduction
  2. Why a Workspace
  3. The Workspace File
  4. Dependency Graph and Linking
  5. A Shared Lockfile and Reproducible Installs
  6. Scripts Across Packages
  7. Catalogs and Version Consistency
  8. Common Pitfalls
  9. Conclusion

Introduction

A monorepo holds several packages that depend on each other, and managing them with separate installs means duplicating dependencies, coordinating versions by hand, and rebuilding the same code repeatedly. A pnpm workspace links the local packages together so a change in one is immediately visible to another, uses a single lockfile for reproducible installs, and stores each dependency version once on disk. This article covers the workspace file, how linking works, how the shared lockfile keeps installs reproducible, running scripts across packages, and the workspace protocol that keeps local versions in sync.

Why a Workspace

Without a workspace, each package installs its own dependencies. The same package version is downloaded into every package’s node_modules, wasting disk and time, and a local package must be published or linked by hand to be used by a sibling. A workspace solves both: dependencies are shared from a store on disk, and local packages are linked so their code is used directly rather than a published copy.

A single lockfile is the other benefit. With separate installs, each package could resolve a different version of a shared dependency, and a change in one lockfile is invisible to the others. In a workspace one lockfile records the resolved graph for all packages, so a single install reproduces the same tree everywhere.

The Workspace File

A pnpm-workspace.yaml lists the directories that contain packages. pnpm discovers packages by that pattern, links the ones that depend on each other, and installs the external dependencies once into a content-addressed store, hard-linked into each package’s node_modules.

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

Dependency Graph and Linking

To depend on a sibling, use the workspace protocol in the dependency range. This tells pnpm to link the local package instead of looking it up in the registry, and it makes the dependency explicit. A published version resolution is avoided, so a change to the sibling is picked up without rebuilding or republishing.

{
  "name": "@acme/ui",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "@acme/utils": "workspace:*",
    "react": "18.3.1"
  }
}

A Shared Lockfile and Reproducible Installs

One pnpm-lock.yaml at the workspace root records every dependency for every package, so a fresh install from the lockfile produces the same tree on any machine. Install with the frozen-lockfile flag in CI so the build fails if the lockfile does not match the manifests, which prevents a silently drifting dependency graph.

The pnpm store keeps a single copy of each package version on disk and hard-links it into the node_modules of every package that uses it, which is why installs in a workspace are fast and disk use is low. This shared store is the mechanism that makes a workspace efficient; a workspace that falls back to copying defeats the purpose.

# Install the whole workspace from the lockfile, failing if it is out of date
pnpm install --frozen-lockfile

# Run a script in one package
pnpm --filter @acme/ui build

# Run a script in every package that has it, in dependency order
pnpm -r build

# Run only in packages changed since a git ref
pnpm --filter "...[origin/main]" test

Scripts Across Packages

The filter flag selects which packages a command applies to, by name, by directory, or by dependency relationship. Run a command recursively across all packages, or only in a package and its dependents, or only in packages changed since a git ref. Running tests only for what changed is the main speed win in a large workspace, because it avoids rebuilding and retesting untouched packages.

Ordering matters: a package that depends on another must be built after it. pnpm’s recursive commands respect the dependency graph when the script declares it, so a build of all packages runs in the right order. Without that, a build can use a stale version of a sibling that has not been rebuilt yet, which produces confusing failures.

Catalogs and Version Consistency

When many packages depend on the same library, pinning the version in each package’s manifest lets them drift. A catalog declares the version once in the workspace file, and packages reference it, so bumping React happens in one place. This keeps versions consistent across packages, which avoids the situation where two packages pull two copies of a library with incompatible types.

Version consistency matters most for libraries with a peer-dependency contract and for type-only packages, where a mismatch produces errors that appear in one package and not another. A catalog makes the shared version explicit and changeable in one place, and the lockfile enforces that every package resolved to it.

# Catalog: declare shared versions once
catalog:
  react: 18.3.1
  zod: 3.23.8

# A package then references the catalog:
#   "dependencies": { "react": "catalog:", "zod": "catalog:" }

Common Pitfalls

Phantom dependencies are the classic workspace hazard: a package imports a library it did not declare, because it is installed at the root or pulled in by a sibling. It works in the monorepo and breaks when the package is published or built in isolation. Declare every dependency a package actually imports, and test a package’s build in isolation to catch this.

Another pitfall is letting the workspace share code that should be versioned, so a change to a shared package forces a rebuild of every consumer. That is the intended behaviour for a tightly coupled monorepo and the wrong choice for packages with independent release cadence. Choose the granularity deliberately, and keep the dependency graph shallow so a change touches few packages.

Conclusion

A pnpm workspace links local packages, shares one lockfile and one store, and runs scripts across the dependency graph. Declare packages in the workspace file, depend on siblings with the workspace protocol, install from a frozen lockfile in CI, filter commands to the packages you changed, and pin shared versions in a catalog. Declare every dependency you import to avoid phantom dependencies, and keep the graph shallow so a change touches few packages. Then a monorepo installs fast, builds in the right order, and stays reproducible.

Sources: pnpm: Workspaces, pnpm: Catalogs, pnpm: Filtering.

FAQ

What does the workspace protocol do?
It tells pnpm to link the local sibling package instead of resolving a version from the registry, so a change to the sibling is picked up without republishing. It makes the local dependency explicit in the manifest.
Why use --frozen-lockfile in CI?
It fails the install if the lockfile does not match the manifests, so a dependency change that was not committed cannot silently alter the build. The lockfile is the reproducible record of the resolved graph.

Related Examples