1
---
2
name: monorepo-management
3
description: Master monorepo management with Turborepo, Nx, and pnpm workspaces to build efficient, scalable multi-package repositories with optimized builds and dependency management. Use when setting up monorepos, optimizing builds, or managing shared dependencies.
4
---
5
6
# Monorepo Management
7
8
Build efficient, scalable monorepos that enable code sharing, consistent tooling, and atomic changes across multiple packages and applications.
9
10
## When to Use This Skill
11
12
- Setting up new monorepo projects
13
- Migrating from multi-repo to monorepo
14
- Optimizing build and test performance
15
- Managing shared dependencies
16
- Implementing code sharing strategies
17
- Setting up CI/CD for monorepos
18
- Versioning and publishing packages
19
- Debugging monorepo-specific issues
20
21
## Core Concepts
22
23
### 1. Why Monorepos?
24
25
**Advantages:**
26
27
- Shared code and dependencies
28
- Atomic commits across projects
29
- Consistent tooling and standards
30
- Easier refactoring
31
- Simplified dependency management
32
- Better code visibility
33
34
**Challenges:**
35
36
- Build performance at scale
37
- CI/CD complexity
38
- Access control
39
- Large Git repository
40
41
### 2. Monorepo Tools
42
43
**Package Managers:**
44
45
- pnpm workspaces (recommended)
46
- npm workspaces
47
- Yarn workspaces
48
49
**Build Systems:**
50
51
- Turborepo (recommended for most)
52
- Nx (feature-rich, complex)
53
- Lerna (older, maintenance mode)
54
55
## Turborepo Setup
56
57
### Initial Setup
58
59
```bash
60
# Create new monorepo
61
npx create-turbo@latest my-monorepo
62
cd my-monorepo
63
64
# Structure:
65
# apps/
66
# web/ - Next.js app
67
# docs/ - Documentation site
68
# packages/
69
# ui/ - Shared UI components
70
# config/ - Shared configurations
71
# tsconfig/ - Shared TypeScript configs
72
# turbo.json - Turborepo configuration
73
# package.json - Root package.json
74
```
75
76
### Configuration
77
78
```json
79
// turbo.json
80
{
81
"$schema": "https://turbo.build/schema.json",
82
"globalDependencies": ["**/.env.*local"],
83
"tasks": {
84
"build": {
85
"dependsOn": ["^build"],
86
"outputs": ["dist/**", ".next/**", "!.next/cache/**"]
87
},
88
"test": {
89
"dependsOn": ["build"],
90
"outputs": ["coverage/**"]
91
},
92
"lint": {
93
"outputs": []
94
},
95
"dev": {
96
"cache": false,
97
"persistent": true
98
},
99
"type-check": {
100
"dependsOn": ["^build"],
101
"outputs": []
102
}
103
}
104
}
105
```
106
107
```json
108
// package.json (root)
109
{
110
"name": "my-monorepo",
111
"private": true,
112
"scripts": {
113
"build": "turbo run build",
114
"dev": "turbo run dev",
115
"test": "turbo run test",
116
"lint": "turbo run lint",
117
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
118
"clean": "turbo run clean && rm -rf node_modules"
119
},
120
"devDependencies": {
121
"turbo": "^2.0.0",
122
"prettier": "^3.0.0",
123
"typescript": "^5.0.0"
124
},
125
"packageManager": "pnpm@8.0.0"
126
}
127
```
128
129
pnpm reads workspace packages only from `pnpm-workspace.yaml`, so declare them there (see `references/details.md`). Turborepo 2 renamed `pipeline` to `tasks` and rejects a `turbo.json` that still uses `pipeline`; run `npx @turbo/codemod migrate` to upgrade a 1.x config.
130
131
### Package Structure
132
133
```json
134
// packages/ui/package.json
135
{
136
"name": "@repo/ui",
137
"version": "0.0.0",
138
"private": true,
139
"main": "./dist/index.js",
140
"types": "./dist/index.d.ts",
141
"exports": {
142
".": {
143
"import": "./dist/index.js",
144
"types": "./dist/index.d.ts"
145
},
146
"./button": {
147
"import": "./dist/button.js",
148
"types": "./dist/button.d.ts"
149
}
150
},
151
"scripts": {
152
"build": "tsup src/index.ts --format esm,cjs --dts",
153
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
154
"lint": "eslint src/",
155
"type-check": "tsc --noEmit"
156
},
157
"devDependencies": {
158
"@repo/tsconfig": "workspace:*",
159
"tsup": "^7.0.0",
160
"typescript": "^5.0.0"
161
},
162
"dependencies": {
163
"react": "^18.2.0"
164
}
165
}
166
```
167
168
## Detailed patterns and worked examples
169
170
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
171
172
## Best Practices
173
174
1. **Consistent Versioning**: Lock dependency versions across workspace
175
2. **Shared Configs**: Centralize ESLint, TypeScript, Prettier configs
176
3. **Dependency Graph**: Keep it acyclic, avoid circular dependencies
177
4. **Cache Effectively**: Configure inputs/outputs correctly
178
5. **Type Safety**: Share types between frontend/backend
179
6. **Testing Strategy**: Unit tests in packages, E2E in apps
180
7. **Documentation**: README in each package
181
8. **Release Strategy**: Use changesets for versioning
182
183
## Common Pitfalls
184
185
- **Circular Dependencies**: A depends on B, B depends on A
186
- **Phantom Dependencies**: Using deps not in package.json
187
- **Incorrect Cache Inputs**: Missing files in Turborepo inputs
188
- **Over-Sharing**: Sharing code that should be separate
189
- **Under-Sharing**: Duplicating code across packages
190
- **Large Monorepos**: Without proper tooling, builds slow down
191
192
## Publishing Packages
193
194
```bash
195
# Using Changesets
196
pnpm add -Dw @changesets/cli
197
pnpm changeset init
198
199
# Create changeset
200
pnpm changeset
201
202
# Version packages
203
pnpm changeset version
204
205
# Publish
206
pnpm changeset publish
207
```
208
209
```yaml
210
# .github/workflows/release.yml
211
- name: Create Release Pull Request or Publish
212
uses: changesets/action@v1
213
with:
214
publish: pnpm release
215
env:
216
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
217
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
218
```
219