Course contentsShow
← JavaScript
Lesson 2551 of 2,72351. Package ManagementFree lesson

Running Scripts Across Workspaces

Execute commands in all or specific workspace packages using built-in and third-party tools.

Running Scripts Across Workspaces

What you'll learn: How to execute npm scripts in all workspace packages at once or target specific ones using your package manager's built-in commands.

The Challenge

In a monorepo with multiple workspace packages, you often need to run the same command everywhere—like running tests, building all packages, or linting code. Doing this manually for each workspace is tedious and error-prone.

Built-In Workspace Commands

Each major package manager provides tools to run scripts across workspaces:

npm (7+):

# Run "test" in all workspaces
npm run test --workspaces

# Run in specific workspaces only
npm run build --workspace=packages/ui --workspace=packages/core

Yarn (classic and berry):

# Run in all workspaces
yarn workspaces run test

# Yarn Berry parallel execution
yarn workspaces foreach run build

pnpm:

# Run recursively in all packages
pnpm run test --recursive

# Filter by package name pattern
pnpm run build --filter "./packages/*"

The Analogy

Think of it like a school principal making an announcement over the intercom versus visiting each classroom individually. The intercom (workspace commands) broadcasts the message efficiently to everyone at once.

Controlling Execution

Most tools let you:

  • Run scripts sequentially (one after another) or in parallel (simultaneously)
  • Filter by package name, location, or dependency relationships
  • Continue or fail fast when one workspace fails

This is crucial when, for example, one package must build before another that depends on it.

Key Takeaway: Workspace-aware script execution lets you orchestrate commands across all packages in your monorepo efficiently, using flags like --workspaces, --recursive, or --filter depending on your package manager.