Skip to main content
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 (the Tree), you can preview every change before it is written to disk.

Creating a generator

1

Add the plugin package if you don't have one yet

2

Scaffold a new generator

This creates the following files inside your plugin:

Generator function signature

Every generator exports a default async function that receives a Tree 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

The schema.json file describes available options, their types, default values, and CLI prompt behavior.

Schema property extensions

Nx recognizes several extensions beyond standard JSON Schema:
Maps positional CLI arguments and workspace context to schema properties.
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.
Controls visibility and order in the Nx Console generator form.Fields are ordered: required > important > regular > deprecated > internal.
Deprecated options appear at the bottom of the Nx Console form with a warning.
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.
Template files use EJS syntax. File names may contain __variable__ placeholders:
Pass helper functions as template variables. For example, { ...options, upper: (s) => s.toUpperCase() } lets templates call <%= upper(name) %>.

Modifying existing files

Update JSON files

String replacement

Running a generator

Always use --dry-run first to review the list of files that will be created or modified before committing to the changes.
When referencing your plugin, use the name field from tools/my-plugin/package.json, not the folder path.

Debugging generators

To use VS Code’s debugger:
  1. Open the Command Palette and choose Debug: Create JavaScript Debug Terminal.
  2. Set breakpoints in generator.ts.
  3. Run nx g my-generator in the debug terminal.

Key devkit utilities for generators