Skip to content

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

Create a Next.js Application for Nx.

Terminal window
nx g app apps/nested/myapp
Terminal window
nx g app apps/myapp --custom-server
Terminal window
nx g app apps/myapp --js

Usage:

Terminal window
nx generate @nx/next:application [options]

Aliases: app

Arguments:

Terminal window
nx generate @nx/next:application <directory> [options]
OptionTypeDescriptionDefault
--appDirbooleanEnable the App Router for this project.true
--customServerbooleanUse a custom Express server for the Next.js application.false
--e2eTestRunnerstringTest runner to use for end to end (E2E) tests."playwright"
--jsbooleanGenerate JavaScript files rather than TypeScript files.false
--linterstringThe tool to use for running lint checks."none"
--namestringThe name of the application.
--rootProjectbooleanCreate an application at the root of the workspace.false
--setParserOptionsProjectbooleanWhether or not to configure the ESLint parserOptions.project option. We do not do this by default for lint performance reasons.false
--skipFormatbooleanSkip formatting files.false
--skipPackageJsonbooleanDo not add dependencies to package.json.false
--srcbooleanGenerate a src directory for the project.true
--stylestringThe file extension to be used for style files."css"
--swcbooleanEnable the Rust-based compiler SWC to compile JS/TS files.true
--tagsstringAdd tags to the application (used for linting).
--unitTestRunnerstringTest runner to use for unit tests."none"
--useProjectJsonbooleanUse a project.json configuration file instead of inlining the Nx configuration in the package.json file.

Create a React Component for Next.

Generate a component named MyComponent at apps/my-app/src/app/my-component/my-component.tsx:

Terminal window
nx g component apps/my-app/src/app/my-component/my-component.tsx
Create a Component with a Different Symbol Name
Section titled “Create a Component with a Different Symbol Name”

Generate a component named Custom at apps/my-app/src/app/my-component/my-component.tsx:

Terminal window
nx g component apps/my-app/src/app/my-component/my-component.tsx --name=custom
Create a Component Omitting the File Extension
Section titled “Create a Component Omitting the File Extension”

Generate a component named MyComponent at apps/my-app/src/app/my-component/my-component.tsx without specifying the file extension:

Terminal window
nx g component apps/my-app/src/app/my-component/my-component

Usage:

Terminal window
nx generate @nx/next:component [options]

Arguments:

Terminal window
nx generate @nx/next:component <path> [options]
OptionTypeDescriptionDefault
--exportbooleanWhen true, the component is exported from the project index.ts (if it exists).false
--jsbooleanGenerate JavaScript files rather than TypeScript files.
--namestringThe component symbol name. Defaults to the last segment of the file path.
--skipFormatbooleanSkip formatting files.false
--skipTestsbooleanWhen true, does not create spec.ts test files for the new component.false
--stylestringThe file extension to be used for style files."css"

Convert existing Next.js project(s) using @nx/next:build executor to use @nx/next/plugin.

Usage:

Terminal window
nx generate @nx/next:convert-to-inferred [options]
OptionTypeDescriptionDefault
--projectstringThe project to convert from using the @nx/next:build executor to use @nx/next/plugin. If not provided, all projects using the @nx/next:build executor will be converted.
--skipFormatbooleanWhether to format files.false

Add a custom server to existing Next.js application.

Terminal window
nx g custom-server my-app

Usage:

Terminal window
nx generate @nx/next:custom-server [options]

Arguments:

Terminal window
nx generate @nx/next:custom-server <project> [options]
OptionTypeDescriptionDefault
--compilerstringThe compiler used to build the custom server."tsc"

Add Cypress Componet Testing to an existing NextJS project.

This generator is designed to get your Next project up and running with Cypress Component Testing.

Terminal window
nx g @nx/next:cypress-component-configuration --project=my-cool-next-project

Running this generator, adds the required files to the specified project with a preconfigured cypress.config.ts designed for Nx workspaces.

cypress.config.ts
import { defineConfig } from 'cypress';
import { nxComponentTestingPreset } from '@nx/next/plugins/component-testing';
export default defineConfig({
component: nxComponentTestingPreset(__filename),
});

Here is an example on how to add custom options to the configuration

cypress.config.ts
import { defineConfig } from 'cypress';
import { nxComponentTestingPreset } from '@nx/next/plugins/component-testing';
export default defineConfig({
component: {
...nxComponentTestingPreset(__filename),
// extra options here
},
});
Terminal window
nx g @nx/next:cypress-component-project --project=my-cool-next-project

You can optionally use the --generate-tests flag to generate a test file for each component in your project.

Terminal window
nx g @nx/next:cypress-component-configuration --project=my-cool-next-project --generate-tests

A new component-test target will be added to the specified project to run your component tests.

Terminal window
nx g component-test my-cool-next-project

Here is an example of the project configuration that is generated.

project.json
{
"targets" {
"component-test": {
"executor": "@nx/cypress:cypress",
"options": {
"cypressConfig": "<path-to-project-root>/cypress.config.ts",
"testingType": "component",
"skipServe": true
}
}
}
}

Nx also supports Angular component testing.

Usage:

Terminal window
nx generate @nx/next:cypress-component-configuration [options]
OptionTypeDescriptionDefault
--projectstring [required]The name of the project to add cypress component testing configuration to
--generateTestsbooleanGenerate default component tests for existing components in the projectfalse
--skipFormatbooleanSkip formatting filesfalse

Create a React Library for an Nx workspace.

Terminal window
nx g lib libs/my-lib

The following will create a library at libs/shared/my-lib.

Terminal window
nx g lib libs/shared/my-lib

Usage:

Terminal window
nx generate @nx/next:library [options]

Aliases: lib

Arguments:

Terminal window
nx generate @nx/next:library <directory> [options]
OptionTypeDescriptionDefault
--appProjectstringThe application project to add the library route to.
--buildablebooleanGenerate a buildable library that uses rollup to bundle.false
--bundlerstringThe bundler to use. Choosing ‘none’ means this library is not buildable."none"
--componentbooleanGenerate a default component.true
--globalCssbooleanWhen true, the stylesheet is generated using global CSS instead of CSS modules (e.g. file is *.css rather than *.module.css).false
--importPathstringThe library name used to import it, like @myorg/my-awesome-lib.
--jsbooleanGenerate JavaScript files rather than TypeScript files.false
--linterstringThe tool to use for running lint checks."none"
--namestringLibrary name
--publishablebooleanCreate a publishable library.
--routingbooleanGenerate library with routes.
--setParserOptionsProjectbooleanWhether or not to configure the ESLint parserOptions.project option. We do not do this by default for lint performance reasons.false
--skipFormatbooleanSkip formatting files.false
--skipPackageJsonbooleanDo not add dependencies to package.json.false
--skipTsConfigbooleanDo not update tsconfig.json for development experience.false
--strictbooleanWhether to enable tsconfig strict mode or not.true
--stylestringThe file extension to be used for style files."css"
--tagsstringAdd tags to the library (used for linting).
--unitTestRunnerstringTest runner to use for unit tests."none"
--useProjectJsonbooleanUse a project.json configuration file instead of inlining the Nx configuration in the package.json file.

Create a Page for Next.

Generate a static page named MyPage at apps/my-app/pages/my-page/page.tsx:

Terminal window
nx g page apps/my-app/pages/my-page

Generate a dynamic page at apps/my-app/pages/products/[id]/page.tsx:

Terminal window
nx g page "apps/my-app/pages/products/[id]"

Usage:

Terminal window
nx generate @nx/next:page [options]

Arguments:

Terminal window
nx generate @nx/next:page <path> [options]
OptionTypeDescriptionDefault
--exportbooleanWhen true, the component is exported from the project index.ts (if it exists).false
--jsbooleanGenerate JavaScript files rather than TypeScript files.false
--namestringThe page symbol name. Defaults to the page directory name.
--skipFormatbooleanSkip formatting files.false
--stylestringThe file extension to be used for style files."css"
--withTestsbooleanWhen true, creates a spec.ts test file for the new page.false

You can get help for any generator by adding the --help flag:

Terminal window
nx generate @nx/next:<generator> --help