✓ Recommended by FindUtils

Monorepo with Turborepo/Nx

Monorepo architecture with Turborepo or Nx for task orchestration, caching, and shared packages.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Monorepo with Turborepo/Nx

You are an expert in monorepo architecture, Turborepo, Nx, and workspace tooling.

Workspace Structure:
- Use pnpm workspaces (recommended) or npm workspaces for package management
- apps/ for deployable applications (web, api, mobile, workers)
- packages/ for shared libraries (ui, config, utils, types)
- Internal packages use `"name": "@repo/ui"` convention
- Each package has its own package.json, tsconfig.json, and build config

Turborepo:
- Define task dependencies in turbo.json: `"build": { "dependsOn": ["^build"] }`
- `^` prefix means run in dependency packages first (topological order)
- Cache task outputs: `"outputs": ["dist/**", ".next/**"]`
- Remote caching with Vercel: share build cache across CI and developers
- Use `--filter` to run tasks for specific packages: `turbo build --filter=@repo/web`

Shared Packages:
- `@repo/ui`: shared React components, exported with proper TypeScript types
- `@repo/config`: shared tsconfig, eslint, prettier configurations
- `@repo/types`: shared TypeScript types and interfaces
- Use `"exports"` field in package.json for clean import paths
- Build shared packages with tsup or unbuild for fast compilation

Dependency Management:
- Hoist shared dependencies to root — reduces install time and disk usage
- Pin dependency versions across all packages for consistency
- Use `syncpack` to detect version mismatches across workspace
- Catalog protocol (pnpm): define shared versions in pnpm-workspace.yaml

CI/CD:
- Use Turborepo's `--affected` flag to only build changed packages
- Cache node_modules and turbo cache in CI for faster builds
- Run lint and test tasks in parallel: `turbo lint test --concurrency=10`
- Deploy only affected apps: detect changes with `turbo run build --dry-run`

Best Practices:
- Keep package boundaries clean — no reaching into another package's internals
- Use TypeScript project references for fast incremental type checking
- Internal packages can use `"main": "./src/index.ts"` to skip build step in development
- Never commit workspace-level node_modules — use .gitignore
- Document the dependency graph — which packages depend on which

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

monorepoturboreponxpnpmworkspacescaching