Monorepo Navigator
Tier: POWERFUL
Category: Engineering
Domain: Monorepo Architecture / Build Systems
Overview
Navigate, manage, and optimize monorepos. Covers Turborepo, Nx, pnpm workspaces, and Lerna. Enables cross-package impact analysis, selective builds/tests on affected packages only, remote caching, dependency graph visualization, and structured migrations from multi-repo to monorepo. Includes Claude Code configuration for workspace-aware development.
Core Capabilities
- - Cross-package impact analysis — determine which apps break when a shared package changes
- Selective commands — run tests/builds only for affected packages (not everything)
- Dependency graph — visualize package relationships as Mermaid diagrams
- Build optimization — remote caching, incremental builds, parallel execution
- Migration — step-by-step multi-repo → monorepo with zero history loss
- Publishing — changesets for versioning, pre-release channels, npm publish workflows
- Claude Code config — workspace-aware CLAUDE.md with per-package instructions
When to Use
Use when:
- - Multiple packages/apps share code (UI components, utils, types, API clients)
- Build times are slow because everything rebuilds when anything changes
- Migrating from multiple repos to a single repo
- Need to publish packages to npm with coordinated versioning
- Teams work across multiple packages and need unified tooling
Skip when:
- - Single-app project with no shared packages
- Team/project boundaries are completely isolated (polyrepo is fine)
- Shared code is minimal and copy-paste overhead is acceptable
Tool Selection
| 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
Turborepo
→ See references/monorepo-tooling-reference.md for details
Common Pitfalls
| Pitfall | Fix |
|---|
Running turbo run build without --filter on every PR | Always use --filter=...[origin/main] in CI |
| INLINECODE4 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 TURBOTEAM 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 |
Best Practices
- 1. Root CLAUDE.md defines the map — document every package, its purpose, and dependency rules
- Per-package CLAUDE.md defines the rules — what's allowed, what's forbidden, testing commands
- Always scope commands with --filter — running everything on every change defeats the purpose
- Remote cache is not optional — without it, monorepo CI is slower than multi-repo CI
- Changesets over manual versioning — never hand-edit package.json versions in a monorepo
- Shared configs in root, extended in packages — tsconfig.base.json, .eslintrc.base.js, jest.base.config.js
- Impact analysis before merging shared package changes — run affected check, communicate blast radius
- Keep packages/types as pure TypeScript — no runtime code, no dependencies, fast to build and type-check
Monorepo Navigator
级别: 强大
类别: 工程
领域: Monorepo 架构 / 构建系统
概述
导航、管理和优化单体仓库。涵盖 Turborepo、Nx、pnpm workspaces 和 Lerna。支持跨包影响分析、仅对受影响包的选择性构建/测试、远程缓存、依赖关系图可视化以及从多仓库到单体仓库的结构化迁移。包含用于工作区感知开发的 Claude Code 配置。
核心能力
- - 跨包影响分析 — 确定当共享包发生更改时哪些应用会中断
- 选择性命令 — 仅对受影响的包运行测试/构建(而非全部)
- 依赖关系图 — 以 Mermaid 图表形式可视化包之间的关系
- 构建优化 — 远程缓存、增量构建、并行执行
- 迁移 — 零历史丢失的逐步多仓库 → 单体仓库迁移
- 发布 — 用于版本管理的 changesets、预发布渠道、npm 发布工作流
- Claude Code 配置 — 包含每包指令的工作区感知 CLAUDE.md
使用时机
适用场景:
- - 多个包/应用共享代码(UI 组件、工具函数、类型定义、API 客户端)
- 构建速度缓慢,因为任何更改都会触发全部重建
- 从多个仓库迁移到单个仓库
- 需要以协调的版本管理向 npm 发布包
- 团队跨多个包协作,需要统一的工具链
跳过场景:
- - 单应用项目,无共享包
- 团队/项目边界完全隔离(多仓库即可)
- 共享代码极少,复制粘贴的开销可以接受
工具选择
| 工具 | 最适合 | 关键特性 |
|---|
| Turborepo | JS/TS 单体仓库,简单流水线配置 | 业界领先的远程缓存,最少配置 |
| Nx |
大型企业,插件生态系统 | 项目图、代码生成、受影响命令 |
|
pnpm workspaces | 工作区协议,磁盘效率 | workspace:* 用于本地包引用 |
|
Lerna | npm 发布,版本管理 | 批量发布,约定式提交 |
|
Changesets | 现代版本管理(优于 Lerna) | 变更日志生成,预发布渠道 |
大多数现代配置:pnpm workspaces + Turborepo + Changesets
Turborepo
→ 详情请参见 references/monorepo-tooling-reference.md
常见陷阱
| 陷阱 | 修复方法 |
|---|
| 每次 PR 运行 turbo run build 时不加 --filter | 在 CI 中始终使用 --filter=...[origin/main] |
| workspace: 引用导致发布失败 |
使用 pnpm changeset publish — 它会自动将 workspace: 替换为真实版本 |
| 无关文件更改导致所有包重建 | 调整 turbo.json 中的 inputs,将文档、配置文件排除在缓存键之外 |
| 共享 tsconfig 导致一个包破坏所有类型检查 | 正确使用 extends — 每个包继承根配置但覆盖 rootDir / outDir |
| 迁移过程中 git 历史丢失 | 合并前使用 git filter-repo --to-subdirectory-filter — 切勿手动移动文件 |
| CI 中远程缓存不工作 | 检查 TURBO
TOKEN 和 TURBOTEAM 环境变量;使用 turbo run build --summarize 验证 |
| CLAUDE.md 过于通用 — Claude 修改了错误的包 | 在每个包的 CLAUDE.md 中添加明确的规则:当处理 X 时,仅修改 apps/X 中的文件 |
最佳实践
- 1. 根目录 CLAUDE.md 定义全局地图 — 记录每个包、其用途和依赖规则
- 每个包的 CLAUDE.md 定义具体规则 — 允许的操作、禁止的操作、测试命令
- 始终使用 --filter 限定命令范围 — 每次更改都运行全部内容违背了初衷
- 远程缓存不是可选项 — 没有它,单体仓库 CI 比多仓库 CI 更慢
- 使用 Changesets 而非手动版本管理 — 切勿在单体仓库中手动编辑 package.json 版本
- 共享配置放在根目录,各包继承扩展 — tsconfig.base.json、.eslintrc.base.js、jest.base.config.js
- 合并共享包更改前进行影响分析 — 运行受影响检查,沟通影响范围
- 保持 packages/types 为纯 TypeScript — 无运行时代码,无依赖,构建和类型检查快速