The point of a monorepo is not that everything lives in one repository. It is that CI stops rebuilding and retesting the whole estate every time someone edits a single package.
Most monorepos I am asked to look at have the first half of that and not the second. Everything is in one place, the pipeline runs build and test across all of it on every push, and a one-line change to a leaf utility triggers forty minutes of CI that recompiles services it could not possibly have touched. That is not a monorepo problem. It is a task-orchestration problem, and it is exactly what Nx exists to solve. This is how to stand up an Nx workspace so a change only rebuilds and retests the projects downstream of it.
The project graph is the thing that matters
Nx works by building a dependency graph of your projects and the tasks you can run against them. It reads your package.json imports, TypeScript path mappings and project configuration, and infers which projects depend on which. Everything else — affected builds, caching, boundary rules — is a consequence of that graph being correct.
Initialise a workspace with pnpm as the package manager:
pnpm dlx create-nx-workspace@latest acme \
--preset=ts \
--packageManager=pnpm
cd acme
# generate a couple of projects to work with
pnpm nx g @nx/js:lib packages/auth
pnpm nx g @nx/js:lib packages/api --directory=packages/api
# see the graph Nx has inferred
pnpm nx graph
nx graph opens the inferred dependency graph in a browser. Before you trust anything downstream, look at it. If two projects that clearly depend on each other are not connected, Nx cannot see the import — usually a missing path mapping or a package referenced by a raw relative path across project boundaries. Fix that first. Affected builds are only as honest as the graph.
nx affected: run only what a change touches
Once the graph is right, nx affected is the payoff. Given a base and a head commit, Nx diffs the files, maps them to projects, walks the graph to find every project that depends on those, and runs the requested target on that set only. Everything upstream of the change is skipped.
# run the test target only on projects affected by the diff
pnpm nx affected -t test --base=origin/main --head=HEAD
# chain several targets in one pass
pnpm nx affected -t lint test build --base=origin/main --head=HEAD
# visualise what a given diff would rebuild
pnpm nx affected --graph --base=origin/main~1 --head=origin/main
The only genuinely fiddly part is choosing the base. Comparing against origin/main on a pull request is fine. On the main branch itself, comparing against HEAD~1 is a trap: if a previous CI run failed, the work it should have done never happened, and you will skip it forever. The right base is the last successful commit on the branch. In GitHub Actions, nrwl/nx-set-shas computes that for you and exports NX_BASE and NX_HEAD, which nx affected reads automatically:
name: ci
on:
push:
branches: [main]
pull_request:
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # affected needs full history
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: pnpm install --frozen-lockfile
# sets NX_BASE and NX_HEAD to the correct SHAs
- uses: nrwl/nx-set-shas@v4
- run: pnpm nx affected -t lint test build
Note fetch-depth: 0. Without full git history the diff is wrong, and a wrong diff quietly under-builds — the worst failure mode, because it is green.
Caching: local first, then remote
Affected reduces the set of projects. Caching reduces the work within that set: if a task ran before against identical inputs, Nx replays the stored result instead of executing it. In current Nx, caching is declared per target in nx.json with named inputs so the hash ignores things that should not invalidate it — a README edit should not bust a build.
{
"namedInputs": {
"default": ["{projectRoot}/**/*", "sharedGlobals"],
"production": [
"default",
"!{projectRoot}/**/*.spec.ts",
"!{projectRoot}/**/*.md"
]
},
"targetDefaults": {
"build": {
"dependsOn": ["^build"],
"inputs": ["production", "^production"],
"cache": true
},
"test": {
"inputs": ["default", "^production"],
"cache": true
},
"lint": { "cache": true }
}
}
dependsOn: ["^build"] tells Nx to build a project’s dependencies first; ^production in the inputs means a project’s hash also folds in its dependencies’ production files. That combination is what makes the cache correct across the graph rather than just per project.
Local caching helps one developer. The multiplier is a remote cache shared across the team and CI: the first agent to build a project populates the cache, and every machine after it — another CI runner, a colleague, tomorrow’s pipeline — replays the artefact instead of rebuilding. Nx offers self-hosted remote-cache plugins backed by your own object storage. The S3 plugin installs and configures itself:
# adds @nx/s3-cache and writes the config block
pnpm nx add @nx/s3-cache
which lands an s3 block in nx.json:
{
"s3": {
"region": "eu-west-1",
"bucket": "acme-nx-cache",
"encryptionKey": "set-your-own-key"
}
}
Credentials come from the standard AWS chain — AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in CI, an SSO profile locally. The bucket must already exist; Nx will not create it. The self-hosted plugins are free to use but require a (free) activation key tied to an Nx account, so read the current licensing before you commit a regulated workload to it — and treat cache artefacts as build outputs that can contain source, so lock the bucket down and set the encryption key rather than leaving it blank. This is the same third-party-dependency discipline that applies to any shared build service; if your services also talk to each other, the same rigour belongs on mutual TLS between internal APIs.
Module boundaries, enforced by the linter
A fast monorepo rots quickly if any project can import any other. Nx enforces architecture with the @nx/enforce-module-boundaries ESLint rule: you tag projects, then declare which tags may depend on which. An import that violates the constraint fails lint — and therefore CI — rather than being caught in review, or not at all. Tags live on each project (in project.json or the package config), and the constraints live in your flat ESLint config:
// eslint.config.mjs
import nx from '@nx/eslint-plugin';
export default [
...nx.configs['flat/base'],
{
files: ['**/*.ts', '**/*.tsx'],
rules: {
'@nx/enforce-module-boundaries': [
'error',
{
depConstraints: [
// feature code may use anything
{ sourceTag: 'scope:api', onlyDependOnLibsWithTags: ['scope:api', 'scope:shared'] },
// shared utilities must stay self-contained
{ sourceTag: 'scope:shared', onlyDependOnLibsWithTags: ['scope:shared'] },
// presentation layers must not reach into data-access directly
{ sourceTag: 'type:ui', onlyDependOnLibsWithTags: ['type:ui', 'type:util'] }
]
}
]
}
}
];
This is where the monorepo earns its keep as an architecture, not just a build optimisation. The graph that powers affected builds is the same graph the boundary rule reads, so the two reinforce each other: clean boundaries keep the dependency graph shallow, and a shallow graph keeps affected sets small. It pairs naturally with a spec-first contract between projects, and the same instinct that keeps global dev tooling isolated with pipx and uv applies here — decide the dependency rules once, then let the tooling refuse to break them.
Stand it up in that order — correct graph, affected on the right base, cache with honest inputs, boundaries in lint — and the pipeline scales with the size of the change, not the size of the repository. Get the base SHA wrong or the graph incomplete, and you will have a monorepo that is green and quietly building nothing it should.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Can you trust the architecture you have?
Architecture diagrams rarely show the reality of how systems actually operate. An independent review establishes what is really there.