pnpm -r, --filter & Publishing
In a monorepo you rarely want to run a command in one package or in all of them. You want "this package and what it depends on", "everything that depends on what I just changed", or "only the packages touched by this pull request". pnpm's --filter selectors express exactly that, and -r runs commands across the workspace in dependency order.
The example workspace
Every session below builds the same four packages:
packages/core @ws/core (no internal deps)
packages/ui @ws/ui -> @ws/core
packages/app @ws/app -> @ws/ui
packages/docs @ws/docs (independent)-r: recursive, in topological order
mkdir -p ws/packages/{core,ui,app,docs} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\n' > pnpm-workspace.yaml
mk() { echo "{ \"name\": \"@ws/$1\", \"version\": \"1.0.0\", \"dependencies\": {$2}, \"scripts\": { \"build\": \"echo build $1\" } }" > packages/$1/package.json; }
mk core ''
mk ui '"@ws/core": "workspace:^"'
mk app '"@ws/ui": "workspace:^"'
mk docs ''
pnpm install > /dev/null
pnpm -r --workspace-concurrency=1 buildScope: 4 of 5 workspace projects
> @ws/core@1.0.0 build /home/learner/ws/packages/core
> echo build core
build core
> @ws/docs@1.0.0 build /home/learner/ws/packages/docs
> echo build docs
build docs
> @ws/ui@1.0.0 build /home/learner/ws/packages/ui
> echo build ui
build ui
> @ws/app@1.0.0 build /home/learner/ws/packages/app
> echo build app
build app
core built before ui, and ui before app: pnpm sorts by the dependency graph. Independent packages (docs) may run in parallel with others when concurrency allows (the default is 4); --workspace-concurrency=1 keeps the output in a fixed order, which these sessions need. pnpm -r on its own skips the root package; add --include-workspace-root if the root has scripts too.
--filter selectors
mkdir -p ws/packages/{core,ui,app,docs} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\n' > pnpm-workspace.yaml
mk() { echo "{ \"name\": \"@ws/$1\", \"version\": \"1.0.0\", \"dependencies\": {$2}, \"scripts\": { \"build\": \"echo build $1\" } }" > packages/$1/package.json; }
mk core ''; mk ui '"@ws/core": "workspace:^"'; mk app '"@ws/ui": "workspace:^"'; mk docs ''
pnpm install > /dev/null
run() { echo "== --filter '$1'"; pnpm --filter "$1" --workspace-concurrency=1 build 2>&1 | grep '^build '; }
run '@ws/ui'
run '@ws/app...'
run '...@ws/core'
run '...^@ws/core'
run './packages/d*'
run '!@ws/docs'== --filter '@ws/ui'
build ui
== --filter '@ws/app...'
build core
build ui
build app
== --filter '...@ws/core'
build core
build ui
build app
== --filter '...^@ws/core'
build ui
build app
== --filter './packages/d*'
build docs
== --filter '!@ws/docs'
build core
build ui
build app
| Selector | Selects |
|---|---|
@ws/ui |
that package (globs like @ws/* work) |
@ws/app... |
the package and its dependencies: everything needed to build it |
...@ws/core |
the package and its dependents: everything a change in it can break |
...^@ws/core |
only the dependents, not the package itself |
@ws/app^... |
only the dependencies |
./packages/d* |
by directory |
!@ws/docs |
everything except |
[origin/main] |
packages with files changed since a git ref |
Multiple --filter flags are combined (union).
Only what changed
The git-based selector is what makes monorepo CI fast: build and test only what a branch touched, plus whatever depends on it.
mkdir -p ws/packages/{core,ui,app,docs} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\n' > pnpm-workspace.yaml
mk() { echo "{ \"name\": \"@ws/$1\", \"version\": \"1.0.0\", \"dependencies\": {$2}, \"scripts\": { \"test\": \"echo test $1\" } }" > packages/$1/package.json; }
mk core ''; mk ui '"@ws/core": "workspace:^"'; mk app '"@ws/ui": "workspace:^"'; mk docs ''
pnpm install > /dev/null
git init -q && git add -A && git commit -qm "initial"
echo 'module.exports = "fixed";' > packages/ui/index.js
git add -A && git commit -qm "fix ui"
echo "== changed since HEAD~1"
pnpm --filter '[HEAD~1]' --workspace-concurrency=1 test 2>&1 | grep '^test '
echo "== changed since HEAD~1, plus dependents"
pnpm --filter '...[HEAD~1]' --workspace-concurrency=1 test 2>&1 | grep '^test '== changed since HEAD~1
test ui
== changed since HEAD~1, plus dependents
test ui
test app
In a pull-request pipeline the ref is the target branch, --filter "...[origin/main]". Turborepo and Nx do the same with caching on top.
Publishing a workspace
pnpm -r publish publishes every non-private package whose current version isn't on the registry yet, in dependency order, rewriting workspace: and catalog: ranges as it goes. It also refuses to publish from a dirty or non-main git branch unless you pass --no-git-checks (used here because the session has no git repository):
mkdir -p ws/packages/{core,ui,app} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\n' > pnpm-workspace.yaml
echo '{ "name": "@ws/core", "version": "1.0.0" }' > packages/core/package.json
echo '{ "name": "@ws/ui", "version": "2.1.0", "dependencies": { "@ws/core": "workspace:^" } }' > packages/ui/package.json
echo '{ "name": "@ws/app", "version": "0.0.1", "private": true, "dependencies": { "@ws/ui": "workspace:*" } }' > packages/app/package.json
pnpm install > /dev/null
pnpm -r publish --no-git-checks 2>&1 | grep -E '^\+ '
echo "--- again, nothing new to publish"
pnpm -r publish --no-git-checks 2>&1 | tail -1
npm view @ws/ui dependencies
npm view @ws/app 2>&1 | grep -m1 E404+ @ws/core@1.0.0
+ @ws/ui@2.1.0
--- again, nothing new to publish
There are no new packages that should be published
{ '@ws/core': '^1.0.0' }
npm error code E404
@ws/app is private, so it was skipped; the second run found nothing new. For real releases, a tool such as Changesets (@changesets/cli) sits on top: contributors add small changeset files describing their changes, and the release step bumps versions, writes changelogs and runs pnpm -r publish.
Test yourself
Which filter selects @ws/core and everything that depends on it?
- @ws/core...
- ...@ws/core
- @ws/core^...
- !@ws/core
Why do this lesson's sessions pass --workspace-concurrency=1?
- pnpm needs it for workspaces
- To run packages one at a time so the output order is stable
- To disable topological sorting
- To skip private packages
What does pnpm -r publish skip?
- Packages with dependencies
- Private packages and versions already on the registry
- Packages using catalog:
- Everything without --filter