Loading article...
Fetching the blog content...
Loading article...
Fetching the blog content...
Complete guide to Turborepo setup and migration. Learn intelligent caching, parallel task execution, and monorepo best practices to speed up your frontend builds dramatically.
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+)
BASHfile.bash1npm install turbo --save-dev 2# or 3yarn add turbo -D 4# or 5pnpm add turbo -D
turbo.jsonCreate a turbo.json file in your repository root. This file defines your task pipeline:
JSONconfig.json1{ 2 "$schema": "https://turbo.build/schema.json", 3 "globalDependencies": ["**/.env.*local"], 4 "tasks": { 5 "build": { 6 "dependsOn": ["^build"], 7 "outputs": [".next/**", "!.next/cache/**", "dist/**"] 8 }, 9 "lint": { 10 "dependsOn": ["^lint"] 11 }, 12 "dev": { 13 "cache": false, 14 "persistent": true 15 }, 16 "start": { 17 "cache": false, 18 "persistent": true 19 } 20 } 21}
package.json ScriptsReplace your existing scripts with Turborepo commands:
JSONconfig.json1{ 2 "scripts": { 3 "dev": "turbo run dev", 4 "build": "turbo run build", 5 "start": "turbo run start", 6 "lint": "turbo run lint", 7 "clean": "turbo run clean && rm -rf node_modules" 8 } 9}
Your individual packages should keep their original scripts. Turborepo will execute these:
JSONconfig.json1// apps/web/package.json 2{ 3 "scripts": { 4 "dev": "next dev", 5 "build": "next build", 6 "start": "next start", 7 "lint": "eslint" 8 } 9}
If you're using shared packages, configure TypeScript path mappings:
JSONconfig.json1// apps/web/tsconfig.json 2{ 3 "compilerOptions": { 4 "paths": { 5 "@/*": ["./src/*"], 6 "@repo/ui": ["../../packages/ui/src"] 7 } 8 } 9}
BASHfile.bash1# Run all dev servers 2npm run dev 3 4# Build all packages 5npm run build 6 7# Lint all packages 8npm run lint
turbo.json configuration filepackage.json scripts to use turbo runturbo.jsonThis project uses Turborepo with the following structure:
TEXTfile.text1frontendfordummies/ 2├── apps/ 3│ └── web/ # Next.js application 4├── packages/ 5│ └── ui/ # Shared UI components 6├── turbo.json # Turborepo configuration 7└── package.json # Root package with workspaces
package.jsonJSONconfig.json1{ 2 "workspaces": [ 3 "apps/*", 4 "packages/*" 5 ], 6 "scripts": { 7 "dev": "turbo run dev", 8 "build": "turbo run build", 9 "start": "turbo run start", 10 "lint": "turbo run lint", 11 "clean": "turbo run clean && rm -rf node_modules" 12 }, 13 "devDependencies": { 14 "turbo": "^2.0.0" 15 }, 16 "packageManager": "npm@10.0.0", 17 "engines": { 18 "node": ">=20" 19 } 20}
Key Points:
turbo.json ConfigurationJSONconfig.json1{ 2 "$schema": "https://turbo.build/schema.json", 3 "globalDependencies": ["**/.env.*local"], 4 "tasks": { 5 "build": { 6 "dependsOn": ["^build"], 7 "outputs": [".next/**", "!.next/cache/**", "dist/**"] 8 }, 9 "lint": { 10 "dependsOn": ["^lint"] 11 }, 12 "dev": { 13 "cache": false, 14 "persistent": true 15 }, 16 "start": { 17 "cache": false, 18 "persistent": true 19 } 20 } 21}
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:
JSONconfig.json1{ 2 "compilerOptions": { 3 "paths": { 4 "@/*": ["./src/*"], 5 "@repo/ui": ["../../packages/ui/src"] 6 } 7 } 8}
This allows importing shared components directly:
TSfile.typescript1import { Button } from "@repo/ui";
Development:
BASHfile.bash1npm run dev
dev scripts in parallelBuilding:
BASHfile.bash1npm run build
packages/ui)Linting:
BASHfile.bash1npm run lint
@repo/ui package can be developed and tested independentlyTo enable remote caching (share cache with your team):
BASHfile.bash1npx turbo login 2npx turbo link
You can create more complex pipelines:
JSONconfig.json1{ 2 "tasks": { 3 "test": { 4 "dependsOn": ["build"], 5 "outputs": ["coverage/**"] 6 }, 7 "type-check": { 8 "dependsOn": ["^build"], 9 "outputs": [] 10 } 11 } 12}
Turborepo can track environment variables:
JSONconfig.json1{ 2 "tasks": { 3 "build": { 4 "env": ["NODE_ENV", "API_URL"] 5 } 6 } 7}
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.
Continue learning with these related challenges
Understanding frontend performance optimization from the ground up - learn how browsers work and optimize your code accordingly.
A practical guide to crawling, indexing, meta tags, semantics, and Core Web Vitals so your pages rank and stay fast.

Understand how browsers render pages through the Critical Rendering Path. Learn to optimize DOM, CSSOM, render tree construction, and reduce time to first paint for faster websites.
Understanding frontend performance optimization from the ground up - learn how browsers work and optimize your code accordingly.
A practical guide to crawling, indexing, meta tags, semantics, and Core Web Vitals so your pages rank and stay fast.

Understand how browsers render pages through the Critical Rendering Path. Learn to optimize DOM, CSSOM, render tree construction, and reduce time to first paint for faster websites.