Nx helps you organize a Next.js monorepo where apps and shared libraries live side by side. The Next.js plugin for Nx, @nx/next, automatically infers build, dev, and start tasks from your Next.js configuration and provides generators for creating applications and libraries.
You don't need the plugin to use Next.js with Nx. Any project already benefits from caching, task orchestration, and the project graph. The plugin adds automatic task inference, code generators, and simplified configuration.
Requirements
Section titled “Requirements”The @nx/next plugin supports the following package versions.
| Package | Supported Versions |
|---|---|
next | >=15.0.0 <17.0.0 |
Nx generators install the latest supported versions automatically when scaffolding new projects.
Setting up @nx/next
Section titled “Setting up @nx/next”Add to an existing Nx workspace
Section titled “Add to an existing Nx workspace”nx add @nx/nextVerify the plugin is setup:
Check for
@nx/nextlisted as a plugin:Terminal window nx reportEnsure inferred project targets are working:
Terminal window nx show projects --with-target=buildInspect a specific project configuration:
Terminal window nx show project my-next-app
Create a new workspace
Section titled “Create a new workspace”Start from the Next.js template to scaffold a Next.js app with a shared UI library:
npx create-nx-workspace@latest my-workspace --template nrwl/nextjs-templateGenerate a new application
Section titled “Generate a new application”nx g @nx/next:app apps/my-appTo start the application in development mode, run nx dev my-app. Read more about the options available for the application generator.
Generate a library
Section titled “Generate a library”nx g @nx/next:lib libs/my-lib
# For a publishable librarynx g @nx/next:lib libs/my-lib --publishable --importPath=@myorg/my-libRead more about the options available for the library generator.
Develop Next.js applications
Section titled “Develop Next.js applications”Serve for development
Section titled “Serve for development”nx dev my-appStarts the Next.js development server at http://localhost:3000 by default.
Build for production
Section titled “Build for production”nx build my-appThe output goes to the .next folder inside your app's project directory by default. Customize the output directory with distDir in your next.config.js:
const nextConfig = { distDir: 'dist',};
module.exports = nextConfig;Start production server
Section titled “Start production server”nx start my-appServes the production build. Depends on build completing first.
Use libraries in your application
Section titled “Use libraries in your application”import { MyComponent } from '@myorg/my-lib';
export default function Index() { return <MyComponent />;}There is no need to build the library prior to using it. When you update your library, the Next.js application automatically picks up the changes.
Configure @nx/next
Section titled “Configure @nx/next”How @nx/next infers tasks
Section titled “How @nx/next infers tasks”The @nx/next plugin creates tasks for any project that has a Next.js configuration file. Any of the following files are recognized:
next.config.jsnext.config.cjsnext.config.mjsnext.config.ts
Plugin options
Section titled “Plugin options”Configure @nx/next/plugin in the plugins array in nx.json:
{ "plugins": [ { "plugin": "@nx/next/plugin", "options": { "buildTargetName": "build", "devTargetName": "dev", "startTargetName": "start", "serveStaticTargetName": "serve-static" } } ]}| Option | Description | Default |
|---|---|---|
buildTargetName | Name of the production build task | build |
devTargetName | Name of the development server task | dev |
startTargetName | Name of the production server task | start |
serveStaticTargetName | Name of the static export serve task | serve-static |
Exclude or include specific projects
Section titled “Exclude or include specific projects”Use include/exclude glob patterns in the plugin configuration to scope which projects the plugin applies to:
{ "plugins": [ { "plugin": "@nx/next/plugin", "include": ["apps/**/*"], "exclude": ["apps/legacy-app/**/*"], "options": { "buildTargetName": "build", "devTargetName": "dev", "startTargetName": "start", "serveStaticTargetName": "serve-static" } } ]}View inferred tasks
Section titled “View inferred tasks”To see what tasks Nx inferred for a project:
nx show project my-appOr open the project details view in Nx Console.
Set up CI for your Next.js monorepo
Section titled “Set up CI for your Next.js monorepo”In CI, Nx runs nx affected to rebuild and retest only the projects a change touches, and caches results to skip repeated work.
For a complete pipeline, see Set up CI.