As your workspace grows, running all tasks for all projects on every PR becomes too slow. The nx affected command solves this by:
- Determining the minimum set of projects affected by your change
- Running tasks only on those projects
This reduces CI compute significantly, and the benefit compounds when paired with remote caching and distributed task execution.
How Nx determines affected projects
Nx combines two sources of information:
- Git diff — Nx checks which files changed between the base and head commits
- Project graph — Nx maps those files to projects, then traverses the graph to find all downstream dependents
For example, if you change a shared utility library, every application and library that depends on it (directly or transitively) is marked as affected.
Run affected tasks
Replace run-many with affected in your commands:
You can also visualize which projects are affected:
Control the comparison range with —base and —head
By default, Nx compares against your main branch. You can override this:
You can also set these as environment variables:
The recommended CI approach is to set --base to the latest successful commit on main. This ensures all changes since the last successful CI run are accounted for, even if multiple commits landed between runs.
Use affected in CI
Here is a GitHub Actions example using nrwl/nx-set-shas to automatically resolve the correct base SHA:
The nrwl/nx-set-shas action sets NX_BASE to the last successful CI commit on the base branch, so only work that has not already passed is re-run.
By default, Nx marks all projects as affected when the package manager lock file changes. To opt into more precise behavior that only marks projects depending on the updated packages:
The projectsAffectedByDependencyUpdates option accepts "auto", "all", or an array of project specifiers.
Ignore files from affected detection
Nx respects two ignore files:
.gitignore — any patterns listed here are excluded from affected detection
.nxignore — an optional Nx-specific ignore file with the same glob syntax
Not using git?
Pass --files to specify changed files explicitly: