Next.jsHard
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(andoutputFileTracingRoot) 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 (
exportsinpackage.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.yaml2// packages: ["apps/*", "packages/*"]34// apps/web/package.json5// { "dependencies": { "@acme/ui": "workspace:*", "next": "16.4.6", "react": "19.2.x" } }67// apps/web/next.config.ts8import path from "node:path";9import type { NextConfig } from "next";1011const nextConfig: NextConfig = {12 transpilePackages: ["@acme/ui"], // ships raw TSX13 turbopack: { root: path.join(__dirname, "../..") },14 outputFileTracingRoot: path.join(__dirname, "../.."),15 output: "standalone",16};1718export default nextConfig;1920// turbo.json21// { "tasks": { "build": { "dependsOn": ["^build"], "outputs": [".next/**", "!.next/cache/**"] },22// "dev": { "cache": false, "persistent": true } } }2324// apps/web/app/page.tsx25// 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.