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
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 workspacesCommon 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.
# 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-nodeand inengines. Native modules resolve different prebuilt binaries per major. CI=truechanges 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
--yesflag explicitly. - Use
npm execornode_modules/.binrather than assuming a globally installed binary is on PATH.