Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nrwl/nx/llms.txt
Use this file to discover all available pages before exploring further.
Code generators are TypeScript functions that accept parameters and automate repetitive development tasks. They help you:
- Scaffold new applications and libraries with consistent structure
- Augment existing projects with new capabilities (e.g. adding Storybook)
- Enforce organization-wide coding standards automatically
Generators are distributed as part of Nx plugins and are invoked with nx generate (or nx g).
Invoke a generator
The syntax is nx g <plugin-name>:<generator-name> [options]:
# Generate a React library
nx g @nx/react:lib packages/mylib
# Generate a React application
nx g @nx/react:application myapp
# Generate a plain TypeScript library
nx g @nx/js:library mylib
You can also specify just the generator name and Nx will prompt you to choose from installed plugins that provide a matching generator:
Preview changes before applying
Use --dry-run to see what files would be created or modified without writing any changes to disk:
nx g @nx/react:lib packages/mylib --dry-run
This is useful for reviewing the impact of a generator before committing to it.
List available generators
To see all generators provided by an installed plugin:
To list all installed plugins and their capabilities:
Interactive mode
When you omit required options, Nx prompts you interactively:
nx g @nx/react:application
# Nx will prompt: What name would you like to use for the application?
# Nx will prompt: Which stylesheet format would you like to use?
You can also run generators through Nx Console, a VS Code / WebStorm extension that provides a visual form-based interface for finding and running generators. Install it from the VS Code Marketplace.
Build your own generator
You can write custom generators to automate your organization’s specific workflows. A generator is a function with the following signature:
// tools/generators/my-generator/generator.ts
import { Tree, formatFiles, installPackagesTask } from '@nx/devkit';
export default async function (tree: Tree, schema: any) {
// Read and write files using the virtual filesystem (tree)
// tree.write('path/to/file.ts', contents);
await formatFiles(tree);
return () => {
installPackagesTask(tree);
};
}
The @nx/devkit package provides utilities for reading/writing files, generating names, running tasks, and more.
Create the generator
Scaffold a new local generator using the @nx/plugin generator:nx g @nx/plugin:plugin tools/my-plugin
nx g @nx/plugin:generator my-generator --project=my-plugin
Implement the generator function
Edit the generated generator.ts file to implement your logic using @nx/devkit utilities.
Run your generator
nx g @my-org/my-plugin:my-generator
See the local generators guide for full documentation on building and testing generators.