Skip to content

The @nx/next plugin provides various executors to help you create and configure next projects within your Nx workspace. Below is a complete reference for all available executors and their options.

build

Build a Next.js app.

project.json:

//...
{
"name": "acme",
"$schema": "node_modules/nx/schemas/project-schema.json",
"sourceRoot": ".",
"projectType": "application",
"targets": {
//...
"build": {
"executor": "@nx/next:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/acme"
}
}
//...
}
}
Terminal window
nx run acme:build

Examples

For Next.js Standalone projects

{% tabs %} {% tab label=“Default configuration” %}

This is the default configuration for Next.js standalone projects. Our @nx/next:build executor is integrated to use Next.js’ CLI. You can read more about the build options at Next.js CLI Options

"build": {
"executor": "@nx/next:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/acme"
},
"configurations": {
"development": {
"outputPath": "."
},
"production": {}
}
},

{% /tab %} {% tab label=“Enable debug” %}

You can create a debug build for more verbose output by:

Using the --debug flag

Terminal window
nx run acme:build:development --debug

Updating the build options to include debug.

"build": {
"executor": "@nx/next:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/acme"
},
"configurations": {
"development": {
"outputPath": ".",
"debug": true
},
"production": {}
}
},
Terminal window
nx run acme:build:development

{% /tab %}

{% tab label=“Adding profiling” %}

You can enable profiing for React by

Using the --profile flag

Terminal window
nx run acme:build:production --profile

Updating the build options to include profile.

"build": {
"executor": "@nx/next:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/acme"
},
"configurations": {
"development": {
"outputPath": ".",
},
"production": {
"profile": true
}
}
},
Terminal window
nx run acme:build:production

{% /tab %}

{% tab label=“Enable experimental app only” %}

Since Next.js 13 the app/ directory it is reserved. You can enable to build only app/ routes by

Using the --experimentalAppOnly flag

Terminal window
nx run acme:build:production --experimentalAppOnly

Updating the build options to include experimentalAppOnly.

"build": {
"executor": "@nx/next:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/acme"
},
"configurations": {
"development": {
"outputPath": ".",
"experimentalAppOnly": true
},
"production": {}
}
},
Terminal window
nx run acme:build:production

{% /tab %}

{% /tabs %}

Options

OptionTypeDescriptionDefault
outputPathstring [required]The output path of the generated files.
buildLibsFromSourcebooleanRead buildable libraries from source instead of building them separately.true
debugbooleanEnable Next.js debug build logging
experimentalAppOnlybooleanOnly build ‘app’ routes
experimentalBuildModestringChange the build mode.
fileReplacementsarrayReplace files with other files in the build.[]
generateLockfilebooleanGenerate a lockfile (e.g. package-lock.json) that matches the workspace lockfile to ensure package versions match.false
includeDevDependenciesInPackageJsonbooleanInclude devDependencies in the generated package.json file. By default only production dependencies are included.false
nextConfigstringPath (relative to workspace root) to a function which takes phase, config, and builder options, and returns the resulting config. This is an advanced option and should not be used with a normal Next.js config file (i.e. next.config.js).
profilebooleanUsed to enable React Production Profiling
skipOverridesbooleanDo not add a overrides and resolutions entries to the generated package.json file. Only works in conjunction with generatePackageJson option.
skipPackageManagerbooleanDo not add a packageManager entry to the generated package.json file.

server

Serve a Next.js app.

project.json:

//...
{
"name": "acme",
"$schema": "node_modules/nx/schemas/project-schema.json",
"sourceRoot": ".",
"projectType": "application",
"targets": {
//...
"serve": {
"executor": "@nx/next:server",
"defaultConfiguration": "production",
"options": {
"buildTarget": "acme:build",
"dev": true
}
}
//...
}
}
Terminal window
nx run acme:serve

Examples

For Next.js Standalone projects

{% tabs %} {% tab label=“Default configuration” %}

This is the default configuration for Next.js standalone projects. Our @nx/next:server executor is integrated to use Next.js’ CLI. You can read more about the serve options at Next.js CLI Options

"serve": {
"executor": "@nx/next:server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "acme:build",
"dev": true
},
"configurations": {
"development": {
"buildTarget": "acme:build:development",
"dev": true
},
"production": {
"buildTarget": "acme:build:production",
"dev": false
}
}
},

{% /tab %} {% tab label=“Enable turbo” %}

Turbopack (beta) is a cutting-edge bundler designed for JavaScript and TypeScript. To read more about support features see Next.js Turbopack Documentation

In the context of Nx, you can utilize Turbopack within both the pages and app directories of Next.js to enhance local development speed. To activate Turbopack, simply:

Append the --turbo flag while executing the Nx development server.

Terminal window
nx run acme:serve --turbo

Updating the build options to include turbo.

"serve": {
"executor": "@nx/next:server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "acme:build",
"dev": true
},
"configurations": {
"development": {
"buildTarget": "acme:build:development",
"dev": true,
"turbo": true
},
//
}
}
Terminal window
nx run acme:serve

{% /tab %}

{% tab label=“Adding keep alive timeout” %}

When using Nx with Next.js behind a downstream proxy, it’s important to make sure that the keep-alive timeouts of Next.js’ HTTP server are set to longer durations than the timeouts of the proxy. If you don’t do this, Node.js will unexpectedly end TCP connections without notifying the proxy when the keep-alive timeout is reached. This can lead to a proxy error when the proxy tries to reuse a connection that Node.js has already terminated.

To configure timeout values (in milliseconds) you can:

Pass --keepAliveTimeout

Terminal window
nx run acme:serve --keepAliveTimeout 60000

Updating the serve options to include keepAliveTimeout.

"serve": {
"executor": "@nx/next:server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "acme:build",
"dev": true
},
"configurations": {
"development": {
"buildTarget": "acme:build:development",
"dev": true,
"keepAliveTimeout": 60000
},
//
}
}
Terminal window
nx run acme:serve

{% /tab %}

{% /tabs %}

Options

OptionTypeDescriptionDefault
buildTargetstring [required]Target which builds the application.
buildLibsFromSourcebooleanRead buildable libraries from source instead of building them separately.true
customServerHttps:booleanEnable HTTPS support for the custom server.
customServerTargetstringTarget which builds the custom server.
devbooleanServe the application in the dev mode.true
experimentalHttpsbooleanEnable HTTPS support for the Next.js development server.
experimentalHttpsCastringPath to a HTTPS certificate authority file.
experimentalHttpsCertstringPath to a HTTPS certificate file.
experimentalHttpsKeystringPath to a HTTPS key file.
hostnamestringHostname on which the application is served.
keepAliveTimeoutnumberMax milliseconds to wait before closing inactive connection.
portnumberPort to listen on.4200
quietbooleanHide error messages containing server information.false
staticMarkupbooleanStatic markup.false
turbobooleanActivate the incremental bundler for Next.js, which is implemented in Rust. Please note, this feature is exclusively available in development mode.