> ## 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.

# Generate Code

> Use Nx generators to scaffold new projects, add features to existing ones, and automate repetitive tasks consistently across your monorepo.

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](/concepts/nx-plugins) and are invoked with `nx generate` (or `nx g`).

## Invoke a generator

The syntax is `nx g <plugin-name>:<generator-name> [options]`:

```bash theme={null}
# 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:

```bash theme={null}
nx g lib packages/mylib
```

## Preview changes before applying

Use `--dry-run` to see what files would be created or modified without writing any changes to disk:

```bash theme={null}
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:

```bash theme={null}
nx list @nx/react
```

To list all installed plugins and their capabilities:

```bash theme={null}
nx list
```

## Interactive mode

When you omit required options, Nx prompts you interactively:

```bash theme={null}
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](https://marketplace.visualstudio.com/items?itemName=nrwl.angular-console).

## 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:

```typescript theme={null}
// 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.

<Steps>
  <Step title="Create the generator">
    Scaffold a new local generator using the `@nx/plugin` generator:

    ```bash theme={null}
    nx g @nx/plugin:plugin tools/my-plugin
    nx g @nx/plugin:generator my-generator --project=my-plugin
    ```
  </Step>

  <Step title="Implement the generator function">
    Edit the generated `generator.ts` file to implement your logic using `@nx/devkit` utilities.
  </Step>

  <Step title="Run your generator">
    ```bash theme={null}
    nx g @my-org/my-plugin:my-generator
    ```
  </Step>
</Steps>

See the [local generators guide](/extending-nx/local-generators) for full documentation on building and testing generators.
