Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Configure and optimize Turborepo monorepo build pipelines with correct task structure, caching, and CI setup.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/best-practices/dependencies.md
1# Dependency Management23Best practices for managing dependencies in a Turborepo monorepo.45## Core Principle: Install Where Used67Dependencies belong in the package that uses them, not the root.89```bash10# Good: Install in specific package11pnpm add react --filter=@repo/ui12pnpm add next --filter=web1314# Avoid: Installing in root15pnpm add react -w # Only for repo-level tools!16```1718## Benefits of Local Installation1920### 1. Clarity2122Each package's `package.json` lists exactly what it needs:2324```json25// packages/ui/package.json26{27"dependencies": {28"react": "^18.0.0",29"class-variance-authority": "^0.7.0"30}31}32```3334### 2. Flexibility3536Different packages can use different versions when needed:3738```json39// packages/legacy-ui/package.json40{ "dependencies": { "react": "^17.0.0" } }4142// packages/ui/package.json43{ "dependencies": { "react": "^18.0.0" } }44```4546### 3. Better Caching4748Installing in root changes workspace lockfile, invalidating all caches.4950### 4. Pruning Support5152`turbo prune` can remove unused dependencies for Docker images.5354## What Belongs in Root5556Only repository-level tools:5758```json59// Root package.json60{61"devDependencies": {62"turbo": "latest",63"husky": "^8.0.0",64"lint-staged": "^15.0.0"65}66}67```6869**NOT** application dependencies:7071- react, next, express72- lodash, axios, zod73- Testing libraries (unless truly repo-wide)7475## Installing Dependencies7677### Single Package7879```bash80# pnpm81pnpm add lodash --filter=@repo/utils8283# npm84npm install lodash --workspace=@repo/utils8586# yarn87yarn workspace @repo/utils add lodash8889# bun90cd packages/utils && bun add lodash91```9293### Multiple Packages9495```bash96# pnpm97pnpm add jest --save-dev --filter=web --filter=@repo/ui9899# npm100npm install jest --save-dev --workspace=web --workspace=@repo/ui101102# yarn (v2+)103yarn workspaces foreach -R --from '{web,@repo/ui}' add jest --dev104```105106### Internal Packages107108```bash109# pnpm110pnpm add @repo/ui --filter=web111112# This updates package.json:113{114"dependencies": {115"@repo/ui": "workspace:*"116}117}118```119120## Keeping Versions in Sync121122### Option 1: Tooling123124```bash125# syncpack - Check and fix version mismatches126npx syncpack list-mismatches127npx syncpack fix-mismatches128129# manypkg - Similar functionality130npx @manypkg/cli check131npx @manypkg/cli fix132133# sherif - Rust-based, very fast134npx sherif135```136137### Option 2: Package Manager Commands138139```bash140# pnpm - Update everywhere141pnpm up --recursive typescript@latest142143# npm - Update in all workspaces144npm install typescript@latest --workspaces145```146147### Option 3: pnpm Catalogs (pnpm 9.5+)148149```yaml150# pnpm-workspace.yaml151packages:152- "apps/*"153- "packages/*"154155catalog:156react: ^18.2.0157typescript: ^5.3.0158```159160```json161// Any package.json162{163"dependencies": {164"react": "catalog:" // Uses version from catalog165}166}167```168169## Internal vs External Dependencies170171### Internal (Workspace)172173```json174// pnpm/bun175{ "@repo/ui": "workspace:*" }176177// npm/yarn178{ "@repo/ui": "*" }179```180181Turborepo understands these relationships and orders builds accordingly.182183### External (npm Registry)184185```json186{ "lodash": "^4.17.21" }187```188189Standard semver versioning from npm.190191## Peer Dependencies192193For library packages that expect the consumer to provide dependencies:194195```json196// packages/ui/package.json197{198"peerDependencies": {199"react": "^18.0.0",200"react-dom": "^18.0.0"201},202"devDependencies": {203"react": "^18.0.0", // For development/testing204"react-dom": "^18.0.0"205}206}207```208209## Common Issues210211### "Module not found"2122131. Check the dependency is installed in the right package2142. Run `pnpm install` / `npm install` to update lockfile2153. Check exports are defined in the package216217### Version Conflicts218219Packages can use different versions - this is a feature, not a bug. But if you need consistency:2202211. Use tooling (syncpack, manypkg)2222. Use pnpm catalogs2233. Create a lint rule224225### Hoisting Issues226227Some tools expect dependencies in specific locations. Use package manager config:228229```yaml230# .npmrc (pnpm)231public-hoist-pattern[]=*eslint*232public-hoist-pattern[]=*prettier*233```234235## Lockfile236237**Required** for:238239- Reproducible builds240- Turborepo dependency analysis241- Cache correctness242243```bash244# Commit your lockfile!245git add pnpm-lock.yaml # or package-lock.json, yarn.lock246```247