Nx Monorepo Setup for Next.js 13 & NestJS 9 API Guide

User avatar placeholder
Written by Tamzid Ahmed

September 7, 2026

Setting up a monorepo with Nx for a Next.js 13 frontend and a NestJS 9 backend streamlines development by sharing code, enabling fast builds, and simplifying dependency management. This guide walks you through initializing an Nx workspace, adding both applications, configuring caching, and leveraging affected commands to only rebuild what changed. By the end, you’ll have a production-ready full-stack monorepo ready for scaling.

Why Choose Nx for a Next.js and NestJS Monorepo

Nx provides a powerful set of tools for monorepo management, including intelligent caching, affected-command detection, and a plugin ecosystem that supports both frontend and backend frameworks. By using Nx, teams can enforce consistent linting, testing, and build pipelines across a Next.js 13 app and a NestJS 9 API, reducing duplication and improving collaboration.

Prerequisites and Installation

Before starting, ensure you have Node.js (>=18) installed, along with npm or Yarn. Install the Nx CLI globally to create workspaces quickly.

  1. Run npm install -g nx or yarn global add nx.
  2. Verify the installation with nx --version.
  3. Make sure Git is initialized in your project folder for affected command tracking.

Initialize the Nx Workspace

Create a new Nx workspace that will host both applications. Choose the empty preset to add apps manually.

  1. Execute npx create-nx-workspace@latest my-monorepo --preset=empty.
  2. When prompted, select a blank workspace and choose npm or Yarn as the package manager.
  3. Navigate into the newly created folder: cd my-monorepo.
  4. Run nx generate @nx/next:app frontend to scaffold a Next.js 13 application.
  5. Run nx generate @nx/nest:app api to add a NestJS 9 API.

Add a Next.js 13 Application

The previous step already created a Next.js app under apps/frontend. You can start it with nx serve frontend. Nx automatically configures Next.js with TypeScript, ESLint, and Jest, and enables module-level caching so subsequent builds reuse outputs.

Add a NestJS 9 API

The NestJS API lives in apps/api. Launch it via nx serve api. Nx wraps the NestJS CLI, providing hot-module replacement during development and leveraging its cache for faster test runs.

Create Shared Libraries for Code Reuse

To avoid duplicating types, utilities, or UI components, generate libraries inside the libs folder.

Example: Shared TypeScript Types

Run nx generate @nx/workspace:lib shared-types --directory=libs/shared. This creates libs/shared/shared-types where you can place DTOs, interfaces, or enum definitions used by both the frontend and API.

Example: UI Component Library

If you prefer a UI kit, generate a React library with nx generate @nx/react:lib ui-components --directory=libs/ui and export components that the Next.js app can import.

By keeping shared code in libraries, Nx’s affected commands correctly detect changes and rebuild only the dependent apps.

Configure Nx Caching for Faster Builds

Nx caches task outputs locally by default and can be extended to remote caches (e.g., Nx Cloud). To verify caching is active, run a build twice and observe the second execution being much faster.

  1. Check nx.json for the targetDefaults section; ensure cache: true for build and test targets.
  2. For remote caching, add nx-cloud and run nx connect-to-nx-cloud to link your workspace.
  3. Monitor cache hits in the terminal output or via Nx Cloud dashboard.

Run Affected Commands to Optimize CI/CD

Affected commands analyze the project graph and determine which apps/libs are impacted by a code change, allowing you to run tests or builds only on those parts.

  1. In CI, replace nx run-many --target=test --all with nx affected:test --base=main --head=$(git rev-parse HEAD).
  2. Similarly, use nx affected:build to create production bundles only for changed applications.
  3. Combine with --parallel and --maxParallel flags to utilize all CPU cores.

Best Practices and Tradeoffs

  • Keep libraries focused and narrowly scoped to maximize cache efficiency.
  • Avoid circular dependencies between libraries; use the project graph (nx graph) to detect them early.
  • Balance the overhead of Nx setup against team size; small projects may not benefit as much from the added tooling.
  • Regularly update Nx plugins to stay compatible with the latest Next.js and NestJS releases.

Conclusion

Setting up a monorepo with Nx for a Next.js 13 frontend and a NestJS 9 API gives you a scalable, high-performance foundation for full-stack development. By leveraging Nx’s caching, affected commands, and library system, you reduce build times, enforce consistency, and ship features faster. Start by cloning the workspace, experiment with shared libraries, and watch your CI pipelines become noticeably more efficient.

Leave a Comment