Overview
This lesson provides a hands-on guide to initializing a new monorepo with Turborepo or integrating Turborepo into an existing workspace. We'll walk through the create-turbo command, understand the initial project structure, and learn how to define and execute tasks within this new high-performance environment. Getting started correctly ensures you can immediately benefit from Turborepo's speed and efficiency.
Key Points
-
Initializing a New Monorepo:
-
Use
npx create-turbo@latestto scaffold a new monorepo with a default structure andturbo.jsonconfiguration. -
It sets up a basic
appsandpackagesdirectory, along with a rootpackage.jsonthat configuresnpm(or Yarn/pnpm) workspaces. -
Integrating into an Existing Monorepo:
-
Install
turboas a dev dependency:npm install turbo --save-dev(oryarn add turbo -D,pnpm add turbo -D). -
Create a
turbo.jsonfile at the root of your monorepo. -
Define your pipeline tasks in
turbo.json, mapping common scripts (e.g.,build,test,lint) to Turborepo's caching and dependency management. -
Basic Task Definition:
-
In
turbo.json, thepipelineobject defines how different script names (like "build" or "test") should behave. -
dependsOn: Specifies task dependencies (e.g.,buildoftendependsOn ^buildto build dependencies first). -
outputs: Defines which files/directories are considered outputs of a task for caching. -
cache: Boolean to enable/disable caching for a task.
Quick Example
bashnpx create-turbo@latest my-turborepo --npm
This command creates a directory my-turborepo with a basic structure:
my-turborepo/
├── apps/
│ ├── web/ # Example Next.js app
│ └── docs/ # Example Next.js docs app
├── packages/
│ ├── ui/ # Example React component library
│ └── config/ # Example shared configuration
└── turbo.json # Turborepo configuration
└── package.json # Root package.json with workspaces
To run tasks: turbo run build will build all projects according to turbo.json.
Summary
Setting up Turborepo, whether from scratch or integrating into an existing project, is straightforward. The create-turbo command simplifies initialization, and the turbo.json file provides a powerful way to define and manage your build pipeline, instantly leveraging Turborepo's performance benefits for your monorepo.