Setting Up a Monorepo with Turborepo and pnpm
Introduction
A monorepo consolidates multiple projects in a single repository. Using Turborepo with pnpm enables fast, consistent builds and code sharing.
Prerequisites
- Node.js >=16
- Git
Step 1: Initialize Repository
git init my-monorepo
cd my-monorepo
pnpm init -y
Step 2: Configure pnpm Workspaces
Add to pnpm-workspace.yaml
:
packages:
- "packages/*"
- "apps/*"
Step 3: Install Turborepo
pnpm add -D turbo
Add to package.json
:
{
"scripts": {
"build": "turbo run build",
"dev": "turbo run dev",
"lint": "turbo run lint"
}
}
Step 4: Create Packages
mkdir -p apps/web packages/ui
cd packages/ui
pnpm init -y
# add shared components here
Step 5: Configure Turborepo
Create turbo.json
:
{
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"dev": {
"cache": false
},
"lint": {}
}
}
Step 6: Scripts and Caching
- Build runs recursively with dependency awareness.
- Cache speeds up CI builds.
Step 7: CI Integration
Example GitHub Actions step:
- uses: pnpm/action-setup@v2
with:
version: 8
- run: pnpm install
- run: pnpm build
Summary
Turborepo with pnpm workspaces enables efficient monorepo management, fast incremental builds, and consistent dependencies across projects.