What are pnpm workspaces?
pnpm workspaces are a mechanism that lets you combine several related projects (packages) into one shared repository (a monorepo) and manage their dependencies centrally. This is convenient when a single repo holds several packages, libraries, or services that share dependencies or depend on each other.
Problems workspaces solve
- Unified dependency management Shared packages are installed once, not in every project.
- Linking packages locally to each other A package can depend on another package in the same repository without publishing it to npm, pnpm creates the local link automatically.
- Optimized installation Installing all dependencies for all packages happens with one command, fast and without duplication.
- Convenience for large projects and monorepos Suits frontend, backend, libraries, and CLIs inside a single repo.
Example workspace structure
javascript
my-monorepo/
pnpm-workspace.yaml
packages/
app/
package.json
ui/
package.jsonThe pnpm-workspace.yaml file:
yaml
packages:
- "packages/*"How it is used
| Action | Command |
|---|---|
| Install dependencies for all packages | pnpm install |
| Run a script in one package | pnpm --filter app dev |
| Add a dependency to a specific package | pnpm add react --filter app |
Summary
pnpm workspaces are a tool for working with monorepos that lets you efficiently manage several packages in one repo, share dependencies, speed up installation, and easily link local packages together.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.