@nx/next - Generators
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.
For an overview of the plugin and setup instructions, see the @nx/next introduction.
application
Section titled “application”Create a Next.js Application for Nx.
Examples
Section titled “Examples”Create app in a nested directory
Section titled “Create app in a nested directory”nx g app apps/nested/myappUse a custom Express server
Section titled “Use a custom Express server”nx g app apps/myapp --custom-serverUse plain JavaScript (not TypeScript)
Section titled “Use plain JavaScript (not TypeScript)”nx g app apps/myapp --jsUsage:
nx generate @nx/next:application [options]Aliases: app
Arguments:
nx generate @nx/next:application <directory> [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--appDir | boolean | Enable the App Router for this project. | true |
--customServer | boolean | Use a custom Express server for the Next.js application. | false |
--e2eTestRunner | string | Test runner to use for end to end (E2E) tests. | "playwright" |
--enableTypedLinting | boolean | Whether to enable typed linting. For flat configs, this configures the recommended parserOptions.projectService and tsconfigRootDir. For legacy .eslintrc configs, this configures parserOptions.project. We do not enable this by default for lint performance reasons. | false |
--js | boolean | Generate JavaScript files rather than TypeScript files. | false |
--linter | string | The tool to use for running lint checks. | "none" |
--name | string | The name of the application. | |
--rootProject | boolean | Create an application at the root of the workspace. | false |
--setParserOptionsProject | boolean | Deprecated alias for enableTypedLinting. | false |
--skipFormat | boolean | Skip formatting files. | false |
--skipPackageJson | boolean | Do not add dependencies to package.json. | false |
--src | boolean | Generate a src directory for the project. | true |
--style | string | The file extension to be used for style files. | "css" |
--swc | boolean | Enable the Rust-based compiler SWC to compile JS/TS files. | true |
--tags | string | Add tags to the application (used for linting). | |
--unitTestRunner | string | Test runner to use for unit tests. | "none" |
--useProjectJson | boolean | Use a project.json configuration file instead of inlining the Nx configuration in the package.json file. |
component
Section titled “component”Create a React Component for Next.
Examples
Section titled “Examples”Create a Component
Section titled “Create a Component”Generate a component named MyComponent at apps/my-app/src/app/my-component/my-component.tsx:
nx g component apps/my-app/src/app/my-component/my-component.tsxCreate 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:
nx g component apps/my-app/src/app/my-component/my-component.tsx --name=customCreate 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:
nx g component apps/my-app/src/app/my-component/my-componentUsage:
nx generate @nx/next:component [options]Arguments:
nx generate @nx/next:component <path> [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--export | boolean | When true, the component is exported from the project index.ts (if it exists). | false |
--js | boolean | Generate JavaScript files rather than TypeScript files. | |
--name | string | The component symbol name. Defaults to the last segment of the file path. | |
--skipFormat | boolean | Skip formatting files. | false |
--skipTests | boolean | When true, does not create spec.ts test files for the new component. | false |
--style | string | The file extension to be used for style files. | "css" |
convert-to-inferred
Section titled “convert-to-inferred”Convert existing Next.js project(s) using @nx/next:build executor to use @nx/next/plugin.
Usage:
nx generate @nx/next:convert-to-inferred [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--project | string | The 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. | |
--skipFormat | boolean | Whether to format files. | false |
custom-server
Section titled “custom-server”Add a custom server to existing Next.js application.
Examples
Section titled “Examples”Add a custom server to existing app
Section titled “Add a custom server to existing app”nx g custom-server my-appUsage:
nx generate @nx/next:custom-server [options]Arguments:
nx generate @nx/next:custom-server <project> [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--compiler | string | The compiler used to build the custom server. | "tsc" |
cypress-component-configuration
Section titled “cypress-component-configuration”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.
nx g @nx/next:cypress-component-configuration --project=my-cool-next-projectRunning this generator, adds the required files to the specified project with a preconfigured cypress.config.ts designed for Nx workspaces.
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
import { defineConfig } from 'cypress';import { nxComponentTestingPreset } from '@nx/next/plugins/component-testing';
export default defineConfig({ component: { ...nxComponentTestingPreset(__filename), // extra options here },});nx g @nx/next:cypress-component-project --project=my-cool-next-projectAuto Generating Tests
Section titled “Auto Generating Tests”You can optionally use the --generate-tests flag to generate a test file for each component in your project.
nx g @nx/next:cypress-component-configuration --project=my-cool-next-project --generate-testsRunning Component Tests
Section titled “Running Component Tests”A new component-test target will be added to the specified project to run your component tests.
nx g component-test my-cool-next-projectHere is an example of the project configuration that is generated.
{ "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:
nx generate @nx/next:cypress-component-configuration [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--project | string [required] | The name of the project to add cypress component testing configuration to | |
--generateTests | boolean | Generate default component tests for existing components in the project | false |
--skipFormat | boolean | Skip formatting files | false |
library
Section titled “library”Create a React Library for an Nx workspace.
Examples
Section titled “Examples”Create a new lib
Section titled “Create a new lib”nx g lib libs/my-libCreate a new lib under a directory
Section titled “Create a new lib under a directory”The following will create a library at libs/shared/my-lib.
nx g lib libs/shared/my-libUsage:
nx generate @nx/next:library [options]Aliases: lib
Arguments:
nx generate @nx/next:library <directory> [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--appProject | string | The application project to add the library route to. | |
--buildable | boolean | Generate a buildable library that uses rollup to bundle. | false |
--bundler | string | The bundler to use. Choosing ‘none’ means this library is not buildable. | "none" |
--component | boolean | Generate a default component. | true |
--enableTypedLinting | boolean | Whether to enable typed linting. For flat configs, this configures the recommended parserOptions.projectService and tsconfigRootDir. For legacy .eslintrc configs, this configures parserOptions.project. We do not enable this by default for lint performance reasons. | false |
--globalCss | boolean | When true, the stylesheet is generated using global CSS instead of CSS modules (e.g. file is *.css rather than *.module.css). | false |
--importPath | string | The library name used to import it, like @myorg/my-awesome-lib. | |
--js | boolean | Generate JavaScript files rather than TypeScript files. | false |
--linter | string | The tool to use for running lint checks. | "none" |
--name | string | Library name | |
--publishable | boolean | Create a publishable library. | |
--routing | boolean | Generate library with routes. | |
--setParserOptionsProject | boolean | Deprecated alias for enableTypedLinting. | false |
--skipFormat | boolean | Skip formatting files. | false |
--skipPackageJson | boolean | Do not add dependencies to package.json. | false |
--skipTsConfig | boolean | Do not update tsconfig.json for development experience. | false |
--strict | boolean | Whether to enable tsconfig strict mode or not. | true |
--style | string | The file extension to be used for style files. | "css" |
--tags | string | Add tags to the library (used for linting). | |
--unitTestRunner | string | Test runner to use for unit tests. | "none" |
--useProjectJson | boolean | Use a project.json configuration file instead of inlining the Nx configuration in the package.json file. |
Create a Page for Next.
Examples
Section titled “Examples”Create a Static Page
Section titled “Create a Static Page”Generate a static page named MyPage at apps/my-app/pages/my-page/page.tsx:
nx g page apps/my-app/pages/my-pageCreate a Dynamic Page
Section titled “Create a Dynamic Page”Generate a dynamic page at apps/my-app/pages/products/[id]/page.tsx:
nx g page "apps/my-app/pages/products/[id]"Usage:
nx generate @nx/next:page [options]Arguments:
nx generate @nx/next:page <path> [options]Options
Section titled “Options”| Option | Type | Description | Default |
|---|---|---|---|
--export | boolean | When true, the component is exported from the project index.ts (if it exists). | false |
--js | boolean | Generate JavaScript files rather than TypeScript files. | false |
--name | string | The page symbol name. Defaults to the page directory name. | |
--skipFormat | boolean | Skip formatting files. | false |
--style | string | The file extension to be used for style files. | "css" |
--withTests | boolean | When true, creates a spec.ts test file for the new page. | false |
Getting Help
Section titled “Getting Help”You can get help for any generator by adding the --help flag:
nx generate @nx/next:<generator> --help