Skip to content
Go back

Setting Up a Monorepo with Turborepo and pnpm

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

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

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.


Share this post on:

Previous Post
Using Vitest and Playwright for Full-Stack Testing
Next Post
Performance Testing and Load Balancing Strategies