yarn workspaces foreach — Yarn CLI reference
Run a command in multiple workspaces
yarn workspaces foreach
Run a command in multiple workspaces
This command selects workspaces from the project and runs the given command in each of them. Workspaces can be selected explicitly, by dependency relationship, by change detection, or by include/exclude patterns:
-
-A,--allselects every workspace of the project. -
-R,--recursiveselects the current workspace and its transitive workspace dependencies (or, combined with--since, the changed workspaces and their transitive dependents). -
-W,--worktreeselects the current workspace and the workspaces nested in it through itsworkspacesfield, recursively. -
--fromselects the workspaces matching the given glob patterns (idents or paths) instead of the current workspace, as starting point for-R,--recursiveand-W,--worktree. -
--sinceselects the workspaces changed since the given ref, or since the refs listed inchangesetBaseRefswhen no ref is given. -
--includeand--excludekeep or drop the workspaces matching the given glob patterns, and--no-privatedrops private workspaces.
By default commands run sequentially. Use -p,--parallel to run them concurrently (up to -j,--jobs at a time, or -j unlimited), and -i,--interlaced to print their output as it is produced rather than once each command exits. With -t,--topological, a command only starts once it has finished in the workspaces the current one depends on (through the dependency kinds set by --followed-dependencies); --topological-dev always takes both dependencies and devDependencies into account.
The lines the commands write to their standard output and to their standard error are both printed on the standard output of yarn workspaces foreach, with the same prefix.
The -v,--verbose flag can be passed up to twice: once to prefix output lines with the originating workspace’s name, and again to include start/finish/timing log lines. Maximum verbosity is enabled by default in terminal environments.
Use -n,--dry-run to print the workspaces the command would run in, in order, without running it.
When both --follow-dependents and --follow-dependencies are set, the dependents are added first and the dependencies are then followed from the whole resulting set, so the selection contains everything needed to build each dependent (equivalent to pnpm’s --filter '...{pkg}...').
If the command is run and the script doesn’t exist in a workspace, that workspace is skipped.
Run the command in every workspace
Start selection from workspaces matching these patterns
Select workspaces changed since a ref, or since the configured base refs when no ref is provided
Compare against this ref rather than the working tree when used with --since
Include workspace dependencies of the selected workspaces
Include the workspaces nested in the selected workspaces
Follow dependencies when expanding the workspace selection
Follow dependents when expanding the workspace selection
Dependency kinds followed by --follow-dependencies
Include only workspaces matching these patterns
Exclude workspaces matching these patterns
Run commands in parallel
Print output from parallel commands as it is produced
Maximum number of parallel jobs, or unlimited
Run workspaces in topological order
Run workspaces in topological order, taking both regular and dev dependencies into account
Increase output verbosity; can be repeated
Include private workspaces
Print the workspaces the command would run in, without running it
Examples#
Run the build script in every workspace
Run the build script in every workspace, in parallel, building workspace dependencies first
Run the build script in two workspaces and all their dependencies, building dependencies first
Publish all public workspaces