Skip to content
Latchkey

yarn workspaces: Usage, Options & Common Errors

Run commands across a Yarn monorepo.

yarn workspace <name> runs a command in one workspace; yarn workspaces (with foreach in Berry) runs across all of them - the core of Yarn monorepo management.

What it does

yarn workspace <name> <cmd> runs a command in a single named workspace. In Berry, yarn workspaces foreach -A <cmd> runs across every workspace (with -pt for parallel, topological order). In Yarn 1, yarn workspaces run <cmd> runs a script in all workspaces.

Common usage

Terminal
yarn workspace @acme/web build           # one workspace
yarn workspaces foreach -A run build     # Berry: all workspaces
yarn workspaces foreach -pt run build    # parallel, topo order
yarn workspaces run test                 # Yarn 1: all workspaces

Common CI error: workspace not found

yarn workspace fails with "Workspace not found" because the name does not match a package.json name, or the workspaces globs in the root package.json do not include the folder. Use the package name field (not the directory), and confirm the root workspaces patterns cover it.

package.json
# root package.json:
"workspaces": ["packages/*", "apps/*"]

Using this in CI

Node tooling on a runner differs from your machine in three ways that matter: CI=true is set, there is no TTY, and the Node major may not be the one you develop on.

  • Pin the Node major with setup-node and in engines. Native modules resolve different prebuilt binaries per major.
  • CI=true changes behaviour in several tools, most often by promoting warnings to errors or disabling interactive prompts.
  • Commands that expect a TTY will hang. Pass the non-interactive or --yes flag explicitly.
  • Use npm exec or node_modules/.bin rather than assuming a globally installed binary is on PATH.

Frequently asked questions

yarn workspaces: Usage, Options & Common Errors?
yarn workspace <name> runs a command in one workspace; yarn workspaces (with foreach in Berry) runs across all of them - the core of Yarn monorepo management.
What it does?
yarn workspace <name> <cmd> runs a command in a single named workspace. In Berry, yarn workspaces foreach -A <cmd> runs across every workspace (with -pt for parallel, topological order). In Yarn 1, yarn workspaces run <cmd> runs a script in all workspaces.
Common CI error: workspace not found?
yarn workspace fails with "Workspace not found" because the name does not match a package.json name, or the workspaces globs in the root package.json do not include the folder. Use the package name field (not the directory), and confirm the root workspaces patterns cover it.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card