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