Loading...
Preparing your page. This should only take a moment.
Loading...
Preparing your page. This should only take a moment.
Turborepo is a high-performance build system for JavaScript and TypeScript monorepos. It's designed to make monorepo development faster and more efficient by intelligently caching and parallelizing tasks across your workspace.
Monorepo Structure: Your project should already be organized as a monorepo with:
package.json with workspaces configuredpackage.json files in each package/appNode.js Version: Ensure you're using Node.js 18+ (this project uses Node.js 20+)
npm install turbo --save-dev # or yarn add turbo -D # or pnpm add turbo -D
turbo.jsonCreate a turbo.json file in your repository root. This file defines your task pipeline:
{ "$schema": "https://turbo.build/schema.json", "globalDependencies": ["**/.env.*local"], "tasks": { "build": { "dependsOn": ["^build"], "outputs": [".next/**", "!.next/cache/**", "dist/**"] }, "lint": { "dependsOn": ["^lint"] }, "dev": { "cache": false, "persistent": true }, "start": { "cache": false, "persistent": true } } }
package.json ScriptsReplace your existing scripts with Turborepo commands:
{ "scripts": { "dev": "turbo run dev", "build": "turbo run build", "start": "turbo run start", "lint": "turbo run lint", "clean": "turbo run clean && rm -rf node_modules" } }
Your individual packages should keep their original scripts. Turborepo will execute these:
// apps/web/package.json { "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "eslint" } }
If you're using shared packages, configure TypeScript path mappings:
// apps/web/tsconfig.json { "compilerOptions": { "paths": { "@/*": ["./src/*"], "@repo/ui": ["../../packages/ui/src"] } } }
# Run all dev servers npm run dev # Build all packages npm run build # Lint all packages npm run lint
turbo.json configuration filepackage.json scripts to use turbo runturbo.jsonThis project uses Turborepo with the following structure:
frontendfordummies/
├── apps/
│ └── web/ # Next.js application
├── packages/
│ └── ui/ # Shared UI components
├── turbo.json # Turborepo configuration
└── package.json # Root package with workspaces
package.json{ "workspaces": [ "apps/*", "packages/*" ], "scripts": { "dev": "turbo run dev", "build": "turbo run build", "start": "turbo run start", "lint": "turbo run lint", "clean": "turbo run clean && rm -rf node_modules" }, "devDependencies": { "turbo": "^2.0.0" }, "packageManager": "npm@10.0.0", "engines": { "node": ">=20" } }
Key Points:
turbo.json Configuration{ "$schema": "https://turbo.build/schema.json", "globalDependencies": ["**/.env.*local"], "tasks": { "build": { "dependsOn": ["^build"], "outputs": [".next/**", "!.next/cache/**", "dist/**"] }, "lint": { "dependsOn": ["^lint"] }, "dev": { "cache": false, "persistent": true }, "start": { "cache": false, "persistent": true } } }
Task Configuration Explained:
build Task:
dependsOn: ["^build"]: Waits for dependencies to build first (the ^ means dependencies)outputs: Specifies what to cache (.next for Next.js, dist for other packages).next/cache/** from caching (Next.js internal cache)lint Task:
dependsOn: ["^lint"]: Lints dependencies firstdev Task:
cache: false: Development servers shouldn't be cachedpersistent: true: Indicates a long-running processstart Task:
cache: false: Production servers shouldn't be cachedpersistent: true: Long-running processglobalDependencies:
["**/.env.*local"]: Changes to .env.local files invalidate all cachesApps (apps/web):
@repo/ui package from the monorepoPackages (packages/ui):
@repo/uiimport { Button } from "@repo/ui"The web app's tsconfig.json includes path mappings for the shared package:
{ "compilerOptions": { "paths": { "@/*": ["./src/*"], "@repo/ui": ["../../packages/ui/src"] } } }
This allows importing shared components directly:
import { Button } from "@repo/ui";
Development:
npm run dev
dev scripts in parallelBuilding:
npm run build
packages/ui)Linting:
npm run lint
@repo/ui package can be developed and tested independentlyTo enable remote caching (share cache with your team):
npx turbo login npx turbo link
You can create more complex pipelines:
{ "tasks": { "test": { "dependsOn": ["build"], "outputs": ["coverage/**"] }, "type-check": { "dependsOn": ["^build"], "outputs": [] } } }
Turborepo can track environment variables:
{ "tasks": { "build": { "env": ["NODE_ENV", "API_URL"] } } }
outputs are correctly specified in turbo.jsonnpx turbo cleandependsOn configuration^ prefix for dependency tasks^ for same-package dependenciestsconfig.json paths are correctly configuredpackage.json and importsGoal: Understand Turborepo and learn how to migrate your monorepo for faster builds and better developer experience.