pnpm Workspaces, workspace: & catalog:
pnpm workspaces combine the ideas of the last few lessons: local packages linked into each other with the workspace: protocol, a strict symlinked layout so each package sees only what it declares, and one shared store. pnpm adds catalogs: dependency versions written once for the whole repository.
pnpm-workspace.yaml
pnpm doesn't use a workspaces field in package.json. The workspace root is marked by pnpm-workspace.yaml, which also holds settings that used to live in .npmrc:
packages:
- "apps/*"
- "packages/*"
catalog:
lodash: 4.18.1
typescript: ~5.9.3
catalogs:
react18:
react: 18.3.1
react-dom: 18.3.1
onlyBuiltDependencies:
- esbuildA workspace in one session
mkdir -p ws/packages/{strings,web} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
cat > pnpm-workspace.yaml <<'YAML'
packages:
- packages/*
catalog:
lodash: 4.18.1
YAML
cat > packages/strings/package.json <<'JSON'
{ "name": "@ws/strings", "version": "1.0.0", "main": "index.js", "dependencies": { "lodash": "catalog:" } }
JSON
echo 'exports.shout = (s) => require("lodash").toUpper(s) + "!";' > packages/strings/index.js
cat > packages/web/package.json <<'JSON'
{ "name": "@ws/web", "version": "1.0.0", "private": true,
"dependencies": { "@ws/strings": "workspace:*" },
"scripts": { "start": "node -e \"console.log(require('@ws/strings').shout('hello pnpm'))\"" } }
JSON
pnpm install
ls -l packages/web/node_modules/@ws | grep -o 'strings -> .*'
ls packages/strings/node_modules
pnpm --filter @ws/web startScope: all 3 workspace projects
Progress: resolved 1, reused 0, downloaded 0, added 0
Packages: +1
+
Progress: resolved 1, reused 0, downloaded 1, added 1, done
Done in 864ms using pnpm v10.34.7
strings -> ../../../strings
lodash
> @ws/web@1.0.0 start /home/learner/ws/packages/web
> node -e "console.log(require('@ws/strings').shout('hello pnpm'))"
HELLO PNPM!
Compared with npm workspaces:
- Each package has its own
node_moduleswith symlinks to exactly its declared dependencies:websees@ws/strings(a symlink to../../../strings, i.e.packages/strings),stringsseeslodash.webcan't uselodashwithout declaring it. - The root gets a single
pnpm-lock.yaml, with oneimportersentry per package. - Internal dependencies use
workspace:. Unlike npm, pnpm 10 does not link a local package for a plain range such as"@ws/strings": "^1.0.0"(thelink-workspace-packagessetting is off by default); it asks the registry instead. The exercises show what that looks like.
The lockfile
mkdir -p ws/packages/{strings,web} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\ncatalog:\n lodash: 4.18.1\n' > pnpm-workspace.yaml
echo '{ "name": "@ws/strings", "version": "1.0.0", "dependencies": { "lodash": "catalog:" } }' > packages/strings/package.json
echo '{ "name": "@ws/web", "version": "1.0.0", "dependencies": { "@ws/strings": "workspace:*", "ms": "^2.1.3" } }' > packages/web/package.json
pnpm install > /dev/null
sed -n '/^catalogs:/,/^packages:/p' pnpm-lock.yamlcatalogs:
default:
lodash:
specifier: 4.18.1
version: 4.18.1
importers:
.: {}
packages/strings:
dependencies:
lodash:
specifier: 'catalog:'
version: 4.18.1
packages/web:
dependencies:
'@ws/strings':
specifier: workspace:*
version: link:../strings
ms:
specifier: ^2.1.3
version: 2.1.3
packages:
importers records each package's specifiers and what they resolved to: link:../strings for the workspace package, a version for registry packages, and for catalog: both the catalog's specifier and the resolved version.
Catalogs: one version, many packages
Without catalogs, upgrading lodash in a 30-package repo means editing 30 package.json files (or writing a Yarn constraint). With a catalog it's one line:
mkdir -p ws/packages/{a,b} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\ncatalog:\n lodash: 4.17.21\n' > pnpm-workspace.yaml
echo '{ "name": "a", "version": "1.0.0", "dependencies": { "lodash": "catalog:" } }' > packages/a/package.json
echo '{ "name": "b", "version": "1.0.0", "dependencies": { "lodash": "catalog:" } }' > packages/b/package.json
pnpm install > /dev/null
pnpm -r list lodash --depth 0 | grep lodash
sed -i 's/lodash: 4.17.21/lodash: 4.18.1/' pnpm-workspace.yaml
pnpm install 2>&1 | grep -E '^[+-] |Already'
pnpm -r list lodash --depth 0 | grep lodash
cd packages/a && pnpm add ms@2.1.3 --save-catalog 2>&1 | grep '^+'
cat package.json && sed -n '/catalog:/,$p' ../../pnpm-workspace.yaml└── lodash@4.17.21
└── lodash@4.17.21
└── lodash@4.18.1
└── lodash@4.18.1
+ ms catalog:
{"name":"a","version":"1.0.0","dependencies":{"lodash":"catalog:","ms":"catalog:"}}
catalog:
lodash: 4.18.1
ms: 2.1.3
pnpm add --save-catalog adds the version to the default catalog and writes catalog: into the package. Named catalogs (catalog:react18) let parts of a repository stay on an older major during a migration, while still sharing one definition.
What gets published
Neither protocol means anything on the registry, so pnpm pack and pnpm publish replace both with real versions:
mkdir -p ws/packages/{strings,web} && cd ws
echo '{ "name": "ws", "private": true }' > package.json
printf 'packages:\n - packages/*\ncatalog:\n lodash: ^4.18.1\n' > pnpm-workspace.yaml
echo '{ "name": "@ws/strings", "version": "1.2.0", "dependencies": { "lodash": "catalog:" } }' > packages/strings/package.json
echo '{ "name": "@ws/web", "version": "1.0.0", "dependencies": { "@ws/strings": "workspace:^" } }' > packages/web/package.json
pnpm install > /dev/null
cd packages/strings && pnpm pack > /dev/null && tar -xzOf ws-strings-1.2.0.tgz package/package.json && echo
cd ../web && pnpm pack > /dev/null && tar -xzOf ws-web-1.0.0.tgz package/package.json{
"name": "@ws/strings",
"version": "1.2.0",
"dependencies": {
"lodash": "^4.18.1"
}
}
{
"name": "@ws/web",
"version": "1.0.0",
"dependencies": {
"@ws/strings": "^1.2.0"
}
}
catalog: became the catalog's range ^4.18.1, and workspace:^ became ^1.2.0, with the same rules as Yarn's (workspace:* → exact, workspace:~ → tilde).
Test yourself
Where does pnpm define which folders are workspace packages?
- The workspaces field in package.json
- pnpm-workspace.yaml
- .npmrc
- pnpm-lock.yaml
What does "lodash": "catalog:" mean?
- The newest lodash
- Use the version defined for lodash in the default catalog of pnpm-workspace.yaml
- Install lodash from a local folder
- Skip lodash when publishing
In a pnpm workspace, can @ws/web use lodash when only @ws/strings declares it?
- Yes, it's hoisted
- No, each package's node_modules contains only its own declared dependencies
- Only in production
- Only with workspace:*