Managing multiple packages or apps in a single repository can simplify dependency management and code sharing.
- Workspaces: NPM/Yarn/PNPM workspaces to link packages locally.
- Turborepo: High-performance build system for monorepos.
Define your project structure in package.json (for NPM/Yarn) or pnpm-workspace.yaml.
// package.json (root)
{
"workspaces": [
"apps/*",
"packages/*"
]
}Add Turbo to your project.
npx init turbo@latestDefine the dependency graph and cacheable tasks.
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": ["^build"], // Build dependencies first
"outputs": ["dist/**", ".next/**"]
},
"test": {
"dependsOn": ["build"],
"outputs": []
},
"lint": {}
}
}Turbo automatically parallelizes tasks and caches results.
# Build all apps and packages
npx turbo run build
# Run tests only for changed packages
npx turbo run test --filter=...[origin/main]- Circular Dependencies: Avoid circular dependencies between packages in the monorepo.
- Hoisting: Be aware of how dependencies are "hoisted" to the root
node_modules. - Versioning: Decide between a "Fixed" versioning strategy (all packages same version) or "Independent" versioning.
A high-performance monorepo where builds and tests are optimized through intelligent caching and parallel execution.