Skip to content

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.

The @nx/next plugin supports the following package versions.

PackageSupported Versions
next>=15.0.0 <17.0.0

Nx generators install the latest supported versions automatically when scaffolding new projects.

Terminal window
nx add @nx/next

Verify the plugin is setup:

  1. Check for @nx/next listed as a plugin:

    Terminal window
    nx report
  2. Ensure inferred project targets are working:

    Terminal window
    nx show projects --with-target=build
  3. Inspect a specific project configuration:

    Terminal window
    nx show project my-next-app

Start from the Next.js template to scaffold a Next.js app with a shared UI library:

Terminal window
npx create-nx-workspace@latest my-workspace --template nrwl/nextjs-template
Terminal window
nx g @nx/next:app apps/my-app

To start the application in development mode, run nx dev my-app. Read more about the options available for the application generator.

Terminal window
nx g @nx/next:lib libs/my-lib
# For a publishable library
nx g @nx/next:lib libs/my-lib --publishable --importPath=@myorg/my-lib

Read more about the options available for the library generator.

Terminal window
nx dev my-app

Starts the Next.js development server at http://localhost:3000 by default.

Terminal window
nx build my-app

The 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:

apps/my-app/next.config.js
const nextConfig = {
distDir: 'dist',
};
module.exports = nextConfig;
Terminal window
nx start my-app

Serves the production build. Depends on build completing first.

apps/my-app/pages/index.tsx
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.

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.js
  • next.config.cjs
  • next.config.mjs
  • next.config.ts

Configure @nx/next/plugin in the plugins array in nx.json:

nx.json
{
"plugins": [
{
"plugin": "@nx/next/plugin",
"options": {
"buildTargetName": "build",
"devTargetName": "dev",
"startTargetName": "start",
"serveStaticTargetName": "serve-static"
}
}
]
}
OptionDescriptionDefault
buildTargetNameName of the production build taskbuild
devTargetNameName of the development server taskdev
startTargetNameName of the production server taskstart
serveStaticTargetNameName of the static export serve taskserve-static

Use include/exclude glob patterns in the plugin configuration to scope which projects the plugin applies to:

nx.json
{
"plugins": [
{
"plugin": "@nx/next/plugin",
"include": ["apps/**/*"],
"exclude": ["apps/legacy-app/**/*"],
"options": {
"buildTargetName": "build",
"devTargetName": "dev",
"startTargetName": "start",
"serveStaticTargetName": "serve-static"
}
}
]
}

To see what tasks Nx inferred for a project:

Terminal window
nx show project my-app

Or open the project details view in Nx Console.

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.