Skip to content

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.

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
Terminal window
npx create-nx-workspace@latest --preset=next
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.

See Set Up CI for a complete CI configuration guide.

Terminal window
nx affected -t build test lint

This uses the project graph to determine which projects are affected by your changes and only runs tasks for those. Read more about the benefits of nx affected.

Share build cache results across your team and CI with remote caching:

Terminal window
nx connect

Next.js applications support static HTML export by setting output: 'export' in next.config.js. The serve-static target serves these static files for E2E testing in CI, without requiring a Next.js server.