Generators are functions that make changes to the file system. They can create new files, update existing ones, add project configuration, and install packages. Because they run against an in-memory virtual file system (theDocumentation 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.
Tree), you can preview every change before it is written to disk.
Creating a generator
Generator function signature
Every generator exports a default async function that receives aTree and a typed options object:
The
Tree is a virtual file system. No changes are written to disk until the generator finishes successfully. This is what makes --dry-run possible.Defining the schema
Theschema.json file describes available options, their types, default values, and CLI prompt behavior.
Schema property extensions
Nx recognizes several extensions beyond standard JSON Schema:$default — dynamic default values
$default — dynamic default values
Maps positional CLI arguments and workspace context to schema properties.
| Source | Description |
|---|---|
{ "$source": "argv", "index": 0 } | First positional CLI argument |
{ "$source": "projectName" } | Current project name; also enables project autocomplete |
{ "$source": "workingDirectory" } | CWD relative to workspace root |
{ "$source": "unparsed" } | Any unmatched extra arguments |
x-prompt — interactive CLI prompts
x-prompt — interactive CLI prompts
Defines a prompt shown when the option is not provided on the command line.Use the shorthand
"x-prompt": "What name would you like?" for a simple text prompt.x-priority — field ordering in Nx Console
x-priority — field ordering in Nx Console
Controls visibility and order in the Nx Console generator form.
Fields are ordered: required > important > regular > deprecated > internal.
| Value | Effect |
|---|---|
"important" | Appears near the top of the form, after required fields |
"internal" | Hidden from the form by default |
x-deprecated — mark options as deprecated
x-deprecated — mark options as deprecated
Deprecated options appear at the bottom of the Nx Console form with a warning.
x-dropdown — populate from workspace data
x-dropdown — populate from workspace data
Tells Nx Console to populate a dropdown with live workspace data.Currently
"projects" is the only supported value.Using generateFiles to template files
generateFiles copies a directory of EJS template files into the workspace, substituting variables along the way.
__variable__ placeholders:
Modifying existing files
Update JSON files
String replacement
Running a generator
name field from tools/my-plugin/package.json, not the folder path.
Debugging generators
To use VS Code’s debugger:- Open the Command Palette and choose Debug: Create JavaScript Debug Terminal.
- Set breakpoints in
generator.ts. - Run
nx g my-generatorin the debug terminal.
Key devkit utilities for generators
| Function | Description |
|---|---|
generateFiles(tree, src, dest, vars) | Copy EJS templates into the workspace |
addProjectConfiguration(tree, name, config) | Register a new project in project.json |
readProjectConfiguration(tree, name) | Read an existing project’s configuration |
updateProjectConfiguration(tree, name, config) | Update an existing project’s configuration |
updateJson(tree, path, fn) | Modify a JSON file with a callback |
readNxJson(tree) | Read nx.json |
updateNxJson(tree, config) | Write changes to nx.json |
formatFiles(tree) | Format all modified files with Prettier |
installPackagesTask(tree) | Trigger package installation after the tree is flushed |
names(str) | Derive camelCase, PascalCase, kebab-case, and underscore variants |
joinPathFragments(...parts) | Safely join path segments |
