Navigates, manages, and optimizes monorepos using Turborepo, Nx, pnpm workspaces, and Changesets with cross-package impact analysis, selective builds, and remote caching. Trigger when: monorepo setup, Turborepo, Nx workspace, pnpm workspaces, Lerna, cross-package impact, selective build, remote cache, migrate multi-repo to monorepo. Do NOT trigger for single-app projects or general dependency management.
Tier: POWERFUL
Category: Engineering
Domain: Monorepo Architecture / Build Systems
Navigate, manage, and optimize monorepos using Turborepo, Nx, pnpm workspaces, and Changesets with cross-package impact analysis, selective builds, remote caching, and migration support.
Apply when the user requests:
Do NOT trigger for:
python3 --versionscripts/monorepo_analyzer.pynode --version to verifypython3 scripts/monorepo_analyzer.py /path/to/monorepo to detect packages, dependency relationships, and build tool configuration--filter=...[origin/main] in CI so only affected packages rebuild on each PRTURBO_TOKEN and TURBO_TEAM env vars; verify with turbo run build --summarizeResults are printed to the user:
### Monorepo Analysis: <root path>
**Tool detected**: <Turborepo | Nx | pnpm workspaces | Lerna>
**Packages**: <count>
**Package Map**:
| Package | Type | Dependents |
|---------|------|------------|
| <name> | app/lib | <list> |
**Affected by last change**: <list>
**Recommendations**:
- <optimization action>
--filter=...[origin/main] in CI — never rebuild all packages on every PRgit filter-repo --to-subdirectory-filter when migrating repos — never move files manually (loses git history)workspace:* for local refs and let Changesets replace them on publishturbo run build without --filter on every CI run — it defeats the purpose of a monorepopackage.json versions — use pnpm changeset for coordinated versioninggit worktree list shows the correct branchpython3 scripts/monorepo_analyzer.py /path/to/monorepo
# → 12 packages detected: 3 apps, 9 libs
# → Affected by change to packages/ui: apps/web, apps/mobile (2 of 3 apps)
# CI: turbo run build test --filter=...[origin/main]
# Bad: rebuilds everything on every PR
turbo run build test
Why this is bad: Rebuilds all 12 packages even when only 1 changed. Wastes CI minutes. Does not use Turborepo's core capability. Remote cache is also bypassed for unchanged packages.
Use when:
Skip when:
| Tool | Best For | Key Feature |
|---|---|---|
| Turborepo | JS/TS monorepos, simple pipeline config | Best-in-class remote caching, minimal config |
| Nx | Large enterprises, plugin ecosystem | Project graph, code generation, affected commands |
| pnpm workspaces | Workspace protocol, disk efficiency | workspace:* for local package refs |
| Lerna | npm publishing, versioning | Batch publishing, conventional commits |
| Changesets | Modern versioning (preferred over Lerna) | Changelog generation, pre-release channels |
Most modern setups: pnpm workspaces + Turborepo + Changesets
→ See references/monorepo-tooling-reference.md for details
python3 scripts/monorepo_analyzer.py /path/to/monorepo
python3 scripts/monorepo_analyzer.py /path/to/monorepo --json
Also see references/monorepo-patterns.md for common architecture and CI patterns.
| Pitfall | Fix |
|---|---|
Running turbo run build without --filter on every PR | Always use --filter=...[origin/main] in CI |
workspace:* refs cause publish failures | Use pnpm changeset publish — it replaces workspace:* with real versions automatically |
| All packages rebuild when unrelated file changes | Tune inputs in turbo.json to exclude docs, config files from cache keys |
| Shared tsconfig causes one package to break all type-checks | Use extends properly — each package extends root but overrides rootDir / outDir |
| git history lost during migration | Use git filter-repo --to-subdirectory-filter before merging — never move files manually |
| Remote cache not working in CI | Check TURBO_TOKEN and TURBO_TEAM env vars; verify with turbo run build --summarize |
| CLAUDE.md too generic — Claude modifies wrong package | Add explicit "When working on X, only touch files in apps/X" rules per package CLAUDE.md |