TExcelTAD

Yarn Workspaces & yarn why

Yarn introduced workspaces to the JavaScript world (in Yarn 1, 2017), and Berry extends them with three things npm lacks: the workspace: protocol for internal dependencies, yarn workspaces foreach with topological ordering, and per-workspace commands that work from anywhere in the repository.

A two-package workspace

bash · try it yourself
mkdir -p mono/packages/{strings,web} && cd mono
echo '{ "name": "mono", "private": true, "packageManager": "yarn@4.18.1", "workspaces": ["packages/*"] }' > package.json
touch yarn.lock
cat > packages/strings/package.json <<'JSON'
{ "name": "@mono/strings", "version": "1.2.0", "main": "index.js",
  "dependencies": { "lodash": "4.17.21" },
  "scripts": { "build": "echo built strings" } }
JSON
echo 'module.exports.slug = (s) => require("lodash").kebabCase(s);' > packages/strings/index.js
cat > packages/web/package.json <<'JSON'
{ "name": "@mono/web", "version": "0.1.0", "private": true,
  "dependencies": { "@mono/strings": "workspace:^", "lodash": "4.18.1" },
  "scripts": { "build": "echo built web", "start": "node -e \"console.log(require('@mono/strings').slug('Yarn Workspaces'))\"" } }
JSON
yarn install
yarn workspaces list
yarn workspace @mono/web run start
➤ YN0000: · Yarn 4.18.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + lodash@npm:4.17.21, lodash@npm:4.18.1
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0013: │ 2 packages were added to the project (+ 2.98 MiB).
➤ YN0000: └ Completed in 0s 217ms
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 324ms
➤ YN0000: .
➤ YN0000: packages/strings
➤ YN0000: packages/web
➤ YN0000: Done in 0s 1ms
yarn-workspaces

workspace:^ says "the local @mono/strings, whatever its version is". Yarn refuses to resolve it from the registry, so a typo or a missing package is an install error instead of a surprise download.

Running scripts across workspaces

bash · try it yourself
mkdir -p mono/packages/{strings,web} && cd mono
echo '{ "name": "mono", "private": true, "packageManager": "yarn@4.18.1", "workspaces": ["packages/*"] }' > package.json
touch yarn.lock
echo '{ "name": "@mono/web", "version": "0.1.0", "dependencies": { "@mono/strings": "workspace:^" }, "scripts": { "build": "echo built web" } }' > packages/web/package.json
echo '{ "name": "@mono/strings", "version": "1.2.0", "scripts": { "build": "echo built strings", "test": "echo tested strings" } }' > packages/strings/package.json
yarn install > /dev/null
yarn workspaces foreach --all --topological run build
echo ---
yarn workspaces foreach --all run test
echo ---
yarn workspaces foreach --recursive --from @mono/web --topological run build
built strings
built web
Done in 0s 40ms
---
tested strings
Done in 0s 27ms
---
built strings
built web
Done in 0s 43ms
Flag Meaning
--all (-A) every workspace, including the root
--topological (-t) dependencies before dependents (strings before web)
--recursive --from <name> the named workspace plus everything it depends on
--since only workspaces changed since the base branch (needs git)
--parallel (-p), --jobs run concurrently; output gets interleaved
--include, --exclude filter by name glob

foreach skips workspaces without the script, so test only ran where it exists.

What happens to workspace: on publish

workspace:^ means nothing outside the repository, so yarn pack and yarn npm publish replace it with a real range based on the local version:

bash · try it yourself
mkdir -p mono/packages/{strings,web} && cd mono
echo '{ "name": "mono", "private": true, "packageManager": "yarn@4.18.1", "workspaces": ["packages/*"] }' > package.json
touch yarn.lock
echo '{ "name": "@mono/strings", "version": "1.2.0" }' > packages/strings/package.json
echo '{ "name": "@mono/web", "version": "0.1.0", "dependencies": { "@mono/strings": "workspace:^", "@mono/x": "workspace:*" } }' > packages/web/package.json
mkdir -p packages/x && echo '{ "name": "@mono/x", "version": "3.0.0" }' > packages/x/package.json
yarn install > /dev/null
cd packages/web && yarn pack --out web.tgz > /dev/null
tar -xzOf web.tgz package/package.json
{
  "name": "@mono/web",
  "version": "0.1.0",
  "dependencies": {
    "@mono/strings": "^1.2.0",
    "@mono/x": "3.0.0"
  }
}
In the repository In the published package (local version 1.2.0)
workspace:^ ^1.2.0
workspace:~ ~1.2.0
workspace:* 1.2.0 (exact)
workspace:^1.0.0 ^1.0.0

pnpm uses the same protocol with the same replacement rules, which you'll see in the pnpm workspaces lesson.

yarn why

Both workspaces above depend on lodash, at different versions. yarn why shows who pulls in each copy:

bash · try it yourself
mkdir -p mono/packages/{strings,web} && cd mono
echo '{ "name": "mono", "private": true, "packageManager": "yarn@4.18.1", "workspaces": ["packages/*"] }' > package.json
touch yarn.lock
echo '{ "name": "@mono/strings", "version": "1.2.0", "dependencies": { "lodash": "4.17.21", "chalk": "^4.1.2" } }' > packages/strings/package.json
echo '{ "name": "@mono/web", "version": "0.1.0", "dependencies": { "@mono/strings": "workspace:^", "lodash": "4.18.1" } }' > packages/web/package.json
yarn install > /dev/null
yarn why lodash
echo ---
yarn why supports-color
echo ---
yarn why has-flag --recursive
├─ @mono/strings@workspace:packages/strings
│  └─ lodash@npm:4.17.21 (via npm:4.17.21)
│
└─ @mono/web@workspace:packages/web
   └─ lodash@npm:4.18.1 (via npm:4.18.1)
---
└─ chalk@npm:4.1.2
   └─ supports-color@npm:7.2.0 (via npm:^7.1.0)
---
├─ @mono/strings@workspace:packages/strings
│  └─ chalk@npm:4.1.2 (via npm:^4.1.2)
│     └─ supports-color@npm:7.2.0 (via npm:^7.1.0)
│        └─ has-flag@npm:4.0.0 (via npm:^4.0.0)
│
└─ @mono/web@workspace:packages/web
   └─ @mono/strings@workspace:packages/strings (via workspace:^)

Plain yarn why shows direct dependents; --recursive shows the full chain from a workspace down. Two lodash versions in one repo is legal but wasteful (two copies in a bundle that uses both workspaces) and usually unintended; the next lesson makes that rule enforceable.

Test yourself

What does "@mono/strings": "workspace:^" become when @mono/web is packed and @mono/strings is at 1.2.0?

  1. workspace:^
  2. ^1.2.0
  3. 1.2.0
  4. *

Which flag makes yarn workspaces foreach run dependencies before dependents?

  1. --all
  2. --parallel
  3. --topological
  4. --recursive

Why does Yarn refuse to resolve a workspace: range from the registry?

  1. Registries don't support scopes
  2. So a missing or misspelled local package is an error rather than a download of a public package
  3. Because workspaces are always private
  4. It's slower