Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
pnpm 10.x reference skill covering workspaces, catalogs, patches, peer deps, overrides, and CI/CD caching strategies.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-workspaces.md
1---2name: pnpm-workspaces3description: Monorepo support with workspaces for managing multiple packages4---56# pnpm Workspaces78pnpm has built-in support for monorepos (multi-package repositories) through workspaces.910## Setting Up Workspaces1112Create `pnpm-workspace.yaml` at the repository root:1314```yaml15packages:16# Include all packages in packages/ directory17- 'packages/*'18# Include all apps19- 'apps/*'20# Include nested packages21- 'tools/*/packages/*'22# Exclude test directories23- '!**/test/**'24```2526## Workspace Protocol2728Use `workspace:` protocol to reference local packages:2930```json31{32"dependencies": {33"@myorg/utils": "workspace:*",34"@myorg/core": "workspace:^",35"@myorg/types": "workspace:~"36}37}38```3940### Protocol Variants4142| Protocol | Behavior | Published As |43|----------|----------|--------------|44| `workspace:*` | Any version | Actual version (e.g., `1.2.3`) |45| `workspace:^` | Compatible version | `^1.2.3` |46| `workspace:~` | Patch version | `~1.2.3` |47| `workspace:^1.0.0` | Semver range | `^1.0.0` |4849## Filtering Packages5051Run commands on specific packages using `--filter`:5253```bash54# By package name55pnpm --filter @myorg/app build56pnpm -F @myorg/app build5758# By directory path59pnpm --filter "./packages/core" test6061# Glob patterns62pnpm --filter "@myorg/*" lint63pnpm --filter "!@myorg/internal-*" publish6465# All packages66pnpm -r build67pnpm --recursive build68```6970### Dependency-based Filtering7172```bash73# Package and all its dependencies74pnpm --filter "...@myorg/app" build7576# Package and all its dependents77pnpm --filter "@myorg/core..." test7879# Both directions80pnpm --filter "...@myorg/shared..." build8182# Changed since git ref83pnpm --filter "...[origin/main]" test84pnpm --filter "[HEAD~5]" lint85```8687## Workspace Commands8889### Install dependencies90```bash91# Install all workspace packages92pnpm install9394# Add dependency to specific package95pnpm --filter @myorg/app add lodash9697# Add workspace dependency98pnpm --filter @myorg/app add @myorg/utils99```100101### Run scripts102```bash103# Run in all packages with that script104pnpm -r run build105106# Run in topological order (dependencies first)107pnpm -r --workspace-concurrency=1 run build108109# Run in parallel110pnpm -r --parallel run test111112# Stream output113pnpm -r --stream run dev114```115116### Execute commands117```bash118# Run command in all packages119pnpm -r exec pwd120121# Run in specific packages122pnpm --filter "./packages/**" exec rm -rf dist123```124125## Workspace Settings126127Configure in `.npmrc` or `pnpm-workspace.yaml`:128129```ini130# Link workspace packages automatically131link-workspace-packages=true132133# Prefer workspace packages over registry134prefer-workspace-packages=true135136# Single lockfile (recommended)137shared-workspace-lockfile=true138139# Workspace protocol handling140save-workspace-protocol=rolling141142# Concurrent workspace scripts143workspace-concurrency=4144```145146## Publishing Workspaces147148When publishing, `workspace:` protocols are converted:149150```json151// Before publish152{153"dependencies": {154"@myorg/utils": "workspace:^"155}156}157158// After publish159{160"dependencies": {161"@myorg/utils": "^1.2.3"162}163}164```165166Use `--no-git-checks` for publishing from CI:167```bash168pnpm publish -r --no-git-checks169```170171## Best Practices1721731. **Use workspace protocol** for internal dependencies1742. **Enable `link-workspace-packages`** for automatic linking1753. **Use shared lockfile** for consistency1764. **Filter by dependencies** when building to ensure correct order1775. **Use catalogs** for shared external dependency versions178179## Example Project Structure180181```182my-monorepo/183├── pnpm-workspace.yaml184├── package.json185├── pnpm-lock.yaml186├── packages/187│ ├── core/188│ │ └── package.json189│ ├── utils/190│ │ └── package.json191│ └── types/192│ └── package.json193└── apps/194├── web/195│ └── package.json196└── api/197└── package.json198```199200<!--201Source references:202- https://pnpm.io/workspaces203- https://pnpm.io/filtering204- https://pnpm.io/npmrc#workspace-settings205-->206