yarn workspaces foreach — Yarn CLI reference

Run a command in multiple workspaces

Docs/Reference/CLI/workspaces/foreach

yarn workspaces foreach

Run a command in multiple workspaces

yarn workspaces foreach <COMMAND> <ARGS…>

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,--all selects every workspace of the project.

  • -R,--recursive selects the current workspace and its transitive workspace dependencies (or, combined with --since, the changed workspaces and their transitive dependents).

  • -W,--worktree selects the current workspace and the workspaces nested in it through its workspaces field, recursively.

  • --from selects the workspaces matching the given glob patterns (idents or paths) instead of the current workspace, as starting point for -R,--recursive and -W,--worktree.

  • --since selects the workspaces changed since the given ref, or since the refs listed in changesetBaseRefs when no ref is given.

  • --include and --exclude keep or drop the workspaces matching the given glob patterns, and --no-private drops 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.

#--all, -Aboolean

Run the command in every workspace

#--fromboolean

Start selection from workspaces matching these patterns

#--sincestring

Select workspaces changed since a ref, or since the configured base refs when no ref is provided

#--headboolean

Compare against this ref rather than the working tree when used with --since

#--recursive, -Rboolean

Include workspace dependencies of the selected workspaces

#--worktree, -Wboolean

Include the workspaces nested in the selected workspaces

#--follow-dependenciesboolean

Follow dependencies when expanding the workspace selection

#--follow-dependentsboolean

Follow dependents when expanding the workspace selection

#--followed-dependenciesboolean

Dependency kinds followed by --follow-dependencies

#--includeboolean

Include only workspaces matching these patterns

#--excludeboolean

Exclude workspaces matching these patterns

#--parallel, -pboolean

Run commands in parallel

#--interlaced, -iboolean

Print output from parallel commands as it is produced

#--jobs, -jboolean

Maximum number of parallel jobs, or unlimited

#--topological, -tboolean

Run workspaces in topological order

#--topological-devboolean

Run workspaces in topological order, taking both regular and dev dependencies into account

#--verbose, -vboolean

Increase output verbosity; can be repeated

#--privateboolean

Include private workspaces

#--dry-run, -nboolean

Print the workspaces the command would run in, without running it

Examples#

Run the build script in every workspace

yarn workspaces foreach -A run build

Run the build script in every workspace, in parallel, building workspace dependencies first

yarn workspaces foreach -Apt run build

Run the build script in two workspaces and all their dependencies, building dependencies first

yarn workspaces foreach -Rpt --from '{workspace-a,workspace-b}' run build

Publish all public workspaces

yarn workspaces foreach -A --no-private npm publish --tolerate-republish