DevToolBoxFREE
BlogAdvertise

Monorepo-verktøy 2026: Turborepo vs Nx vs Lerna vs pnpm Workspaces

15 minby DevToolBox

Et monorepo forbedrer kodedeling, refaktorering og avhengighetsstyring.

Turborepo: oppsett og struktur

Turborepo er optimert for JavaScript/TypeScript-monorepos.

# Create a new Turborepo monorepo
npx create-turbo@latest my-monorepo
cd my-monorepo

# Or add Turborepo to an existing monorepo
npm install turbo --save-dev

# Directory structure
my-monorepo/
├── apps/
│   ├── web/          # Next.js app
│   └── docs/         # Docs site
├── packages/
│   ├── ui/           # Shared React components
│   ├── utils/        # Shared utilities
│   └── tsconfig/     # Shared TypeScript configs
├── turbo.json        # Pipeline configuration
└── package.json      # Root workspace config

Turborepo pipeline-konfigurasjon

turbo.json-pipelinen definerer hvordan oppgaver er relatert til hverandre.

// turbo.json — pipeline definition
{
    "$schema": "https://turbo.build/schema.json",
    "pipeline": {
        "build": {
            "dependsOn": ["^build"],   // ^ means: run in dependency order
            "outputs": [".next/**", "!.next/cache/**", "dist/**"]
        },
        "test": {
            "dependsOn": ["^build"],
            "inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts"]
        },
        "lint": {
            "outputs": []
        },
        "dev": {
            "cache": false,            // Never cache dev servers
            "persistent": true         // Long-running task
        },
        "type-check": {
            "dependsOn": ["^build"],
            "outputs": []
        }
    },
    "globalEnv": ["NODE_ENV", "DATABASE_URL"]
}

# Run all build tasks (uses cache if inputs unchanged)
npx turbo build

# Run only for specific apps/packages
npx turbo build --filter=web
npx turbo build --filter=...ui  # ui and all its dependents

# Force re-run (bypass cache)
npx turbo build --force

# View task graph
npx turbo build --graph

Nx: oppsett og prosjektgraf

Nx tilbyr et rikere funksjonssett: kodegeneratorer, affected-kommandoer.

# Create Nx monorepo
npx create-nx-workspace@latest my-nx-repo --preset=ts

# Add Nx to existing monorepo
npx nx@latest init

# Generate a new app or library
nx generate @nx/next:app web
nx generate @nx/react:library ui
nx generate @nx/node:library utils

# nx.json — workspace configuration
{
    "tasksRunnerOptions": {
        "default": {
            "runner": "nx/tasks-runners/default",
            "options": {
                "cacheableOperations": ["build", "test", "lint", "e2e"],
                "parallel": 3
            }
        }
    },
    "targetDefaults": {
        "build": {
            "dependsOn": ["^build"],
            "inputs": ["production", "^production"]
        }
    },
    "namedInputs": {
        "default": ["{projectRoot}/**/*", "sharedGlobals"],
        "production": ["default", "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)"],
        "sharedGlobals": []
    }
}

pnpm Workspaces: lettvektsapproach

pnpm workspaces gir innebygd monorepo-støtte uten ekstra verktøy.

# pnpm-workspace.yaml
packages:
  - 'apps/*'
  - 'packages/*'

# Install all workspace dependencies
pnpm install

# Add a package to a specific workspace
pnpm add react --filter @myapp/web

# Add a local workspace package as dependency
# In apps/web/package.json:
{
    "dependencies": {
        "@myapp/ui": "workspace:*"
    }
}

# Run scripts across all packages
pnpm --filter '*' run build
pnpm --filter './apps/*' run dev
pnpm --filter '...@myapp/ui' run test  # ui and dependents

# Publish all public packages
pnpm publish -r --access public

Ekstern cache

Ekstern caching deler byggeartefakter mellom utviklere og CI.

# Turborepo Remote Cache (Vercel)
# Enables sharing build cache across developers and CI

# 1. Link to Vercel
npx turbo login
npx turbo link

# 2. Now CI builds share cache with local dev:
# CI run: builds from scratch, uploads to cache
# Developer: pulls cache, gets instant builds

# Self-host with Turborepo Remote Cache (open source)
# docker run -p 3000:3000 ducktors/turborepo-remote-cache

# .turbo/config.json (auto-generated by turbo link)
{
    "teamId": "team_xxx",
    "apiUrl": "https://vercel.com"
}

# Nx Cloud (similar offering from Nx)
# nx connect-to-nx-cloud
# Provides remote caching + task distribution

Versjonering med Changesets

Changesets er standardverktøyet for versjonering og publisering i monorepos.

# Changesets — versioning and publishing for monorepos
# Works with npm/yarn/pnpm workspaces

pnpm add -D @changesets/cli
pnpm changeset init

# When you make a change that needs a version bump:
pnpm changeset
# → Interactive prompt: which packages changed, bump type (major/minor/patch)

# Creates a markdown file in .changeset/ describing the change
# Example .changeset/funny-bears-dance.md:
---
"@myapp/ui": minor
"@myapp/utils": patch
---

Add new Button component with loading state

# Apply changesets (bumps versions, updates CHANGELOG.md)
pnpm changeset version

# Publish all changed packages
pnpm changeset publish

Turborepo vs Nx vs pnpm Workspaces

FeatureTurborepoNxpnpm Workspaces
Setup complexityLowMediumVery Low
Task cachingBuilt-inBuilt-inManual/external
Remote cacheVercel (free tier)Nx Cloud (paid)None built-in
Code generatorsNoYes (rich)No
Affected detectionBasic (--filter)Advanced (nx affected)Via Changesets
Language supportJS/TS focusedPolyglotAny
Learning curveLowMedium-HighLow

Beste praksis

  • Starte med pnpm workspaces + Turborepo.
  • Definere klare grenser: delte pakker i packages/, apper i apps/.
  • Bruke Changesets for versjonering.
  • Aktivere ekstern cache tidlig.
  • Holde hvert package.json eksplisitt.

Ofte stilte spørsmål

Turborepo eller Nx?

Turborepo er enklere for de fleste JS-prosjekter. Nx har flere funksjoner for store repos.

Monorepo vs polyrepo?

Et monorepo lagrer all kode i ett enkelt repository.

Hvordan fungerer Turborepo-caching?

Turborepo hasher input for hvert oppdrag og gjenoppretter output fra cache.

Forskjellige pakkebehandlere i monorepo?

Nei. Alle workspaces må bruke samme pakkebehandler.

Miljøvariabler i monorepo?

Definere variabler per app og liste dem i turbo.json globalEnv.

Var dette nyttig?

Stay Updated

Get weekly dev tips and new tool announcements.

No spam. Unsubscribe anytime.

Partner Picks

Sponsor this article

Place your product next to this developer topic with tracked clicks.

Ask about article sponsorship

This site uses cookies for analytics and to display ads. By continuing to browse, you agree. Privacy Policy