How do you implement a monorepo with Next.js?

Use a workspace-aware package manager (pnpm, npm or Yarn workspaces, or Bun) with a task runner such as Turborepo or Nx. Put apps in apps/ and shared code in packages/, depend on internal packages via workspace:*, and let Next.js compile untranspiled internal packages with transpilePackages or by having those packages build themselves.

Typical layout

apps/web        Next.js app
apps/docs       another Next.js app
packages/ui     shared React components
packages/config shared tsconfig, ESLint, Tailwind presets
packages/db     Prisma or Drizzle client (server-only)

Next.js configuration

  • transpilePackages: ["@acme/ui"] when internal packages ship raw TypeScript/JSX ("just-in-time" packages).
  • turbopack.root (and outputFileTracingRoot) set to the monorepo root so Turbopack and standalone output can find hoisted dependencies.
  • Tailwind v4: add @source "../../packages/ui"; so classes used in shared components are generated.
  • Symlinked packages outside the root: experimental.turbopackAdditionalRoots (16.4).

Task running and caching

Turborepo caches next build outputs (.next/** excluding .next/cache/**) locally and remotely, and runs only affected apps. Next.js's own Turbopack disk cache also speeds CI when .next/cache is cached.

Good practices

  • Shared packages export clear entry points (exports in package.json), with "use client" at the top of client component files.
  • Mark server-only packages with import "server-only".
  • Keep one version of React and Next.js across apps (pnpm catalogs or syncpack).
  • For multiple apps on one domain, use multi-zones with rewrites or assetPrefix.

Code example

tsx
1// pnpm-workspace.yaml
2// packages: ["apps/*", "packages/*"]
3
4// apps/web/package.json
5// { "dependencies": { "@acme/ui": "workspace:*", "next": "16.4.6", "react": "19.2.x" } }
6
7// apps/web/next.config.ts
8import path from "node:path";
9import type { NextConfig } from "next";
10
11const nextConfig: NextConfig = {
12 transpilePackages: ["@acme/ui"], // ships raw TSX
13 turbopack: { root: path.join(__dirname, "../..") },
14 outputFileTracingRoot: path.join(__dirname, "../.."),
15 output: "standalone",
16};
17
18export default nextConfig;
19
20// turbo.json
21// { "tasks": { "build": { "dependsOn": ["^build"], "outputs": [".next/**", "!.next/cache/**"] },
22// "dev": { "cache": false, "persistent": true } } }
23
24// apps/web/app/page.tsx
25// import { Button } from "@acme/ui/button";

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app