How to understand a pnpm workspaces codebase
pnpm workspaces let several packages live in one repository and depend on each other through `workspace:` versions. There may be no extra build tool; scripts are run across packages with `pnpm -r` or `--filter`.
How to tell it's pnpm workspaces
pnpm-workspace.yamland apnpm-lock.yamlat the root- Dependencies like
"@scope/utils": "workspace:*" - Root scripts using
pnpm -rorpnpm --filter
Where things usually live
- pnpm-workspace.yaml
- Package globs and, optionally, version catalogs.
- packages/*
- Workspace packages, each with its own
package.json. - package.json (root)
- Shared dev tooling and scripts.
- .npmrc
- Settings such as hoisting and peer dependency behaviour.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
pnpm-workspace.yaml, to list packages- Each package's
package.jsonexports,mainand scripts
How a request moves through the code
pnpm installlinks workspace packages into each other'snode_modules.- A package imports another by name and gets the local folder.
pnpm -r buildruns builds across packages in dependency order.- On publish,
workspace:versions are replaced with real version numbers.
Questions to ask about the repository
- Which packages depend on which?
- Which packages are published and which are private?
- Are dependency versions centralised in a catalog?
What trips people up
- pnpm doesn't hoist dependencies by default, so code that relies on undeclared dependencies breaks; that's usually the fix, not a pnpm bug.
- Packages consumed as TypeScript source need the consumer's build to compile them.