Nx generate

From tech-leads-club/agent-skills · View interactive version

Nx generate is an AI agent skill from the GitHub repository tech-leads-club/agent-skills. Generate code using Nx generators — scaffold projects, libraries, features, or run workspace-specific generators with proper discovery, validation, and verification. Use when user says "create a new library", "scaffold a component", "generate code with Nx", "run a generator", "nx generate", or any c

How to use this skill

Skill file

Run Nx Generator

Nx generators are powerful tools that scaffold projects, make automated code migrations or automate repetitive tasks in a monorepo. They ensure consistency across the codebase and reduce boilerplate work.

This skill applies when the user wants to:

Generator Discovery Flow

Step 1: List Available Generators

Use the Nx CLI to discover available generators:

This includes:

Step 2: Match Generator to User Request

Based on the user's request, identify which generator(s) could fulfill their needs. Consider:

IMPORTANT: When both a local workspace generator and an external plugin generator could satisfy the request, always prefer the local workspace generator. Local generators are customized for the specific repo's patterns and conventions.

It's possible that the user request is something that no Nx generator exists for whatsoever. In this case, you can stop using this skill and try to help the user another way. HOWEVER, the burden of proof for this is high. Before aborting, carefully consider each and every generator that's available. Look into details for any that could be related in any way before making this decision.

Pre-Execution Checklist

Before running any generator, complete these steps:

1. Fetch Generator Schema

Use the --help flag to understand all available options:

npx nx g @nx/react:library --help

Pay attention to:

2. Read Generator Source Code

Understanding what the generator actually does helps you:

To find generator source code:

2.5 Reevaluate if the generator is right

Once you have built up an understanding of what the selected generator does, reconsider: Is this the right generator to service the user request?

If not, it's okay to go back to the Generator Discovery Flow and select a different generator before proceeding. If you do, make sure to go through the entire pre-execution checklist once more.

3. Understand Repo Context

Before generating, examine the target area of the codebase:

For example, if similar libraries are using a specific test runner, build tool or linter, try to match that if possible.

If projects or other artifacts are organized with a specific naming convention, try to match it.

4. Validate Required Options

Ensure all required options have values:

Execution

Keep in mind that you might have to prefix things with npx/pnpx/yarn if the user doesn't have nx installed globally.

Many generators will behave differently based on where they are executed. For example, first-party nx library generators use the cwd to determine the directory that the library should be placed in. This is highly important.

Consider Dry-Run (Optional)

Running with --dry-run first is strongly encouraged but not mandatory. Use your judgment:

Running the Generator

Execute the generator with:

nx generate <generator-name> <options> --no-interactive

CRITICAL: Always include --no-interactive to prevent prompts that would hang the execution.

Example:

nx generate @nx/react:library --name=my-utils --no-interactive

Handling Generator Failures

If the generator fails:

  1. Diagnose the error - Read the error message carefully
  2. Identify the cause - Missing options, invalid values, conflicts, etc.
  3. Attempt automatic fix - Adjust options or resolve conflicts
  4. Retry - Run the generator again with corrected options

Common failure reasons:

Post-Generation

1. Modify Generated Code (If Needed)

Generators provide a starting point, but the output may need adjustment to match the user's specific requirements:

2. Format Code

Run formatting on all generated/modified files:

nx format --fix

Languages other than javascript/typescript might need other formatting invocations too.

3. Run Verification

Verify that the generated code works correctly. What this looks like will vary depending on the type of generator and the targets available.

If the generator created a new project, run its targets directly

Use your best judgement to determine what needs to be verified.

Example:

nx lint <new-project>
nx test <new-project>
nx build <new-project>

4. Handle Verification Failures

When verification fails:

If scope is manageable (a few lint errors, minor type issues):

If issues are extensive (many errors, complex problems):

Error Handling

Generator Failures

Missing Options

Key Principles

  1. Local generators first - Always prefer workspace/local generators over external plugin generators when both could work
  1. Understand before running - Read both the schema AND the source code to fully understand what will happen
  1. No prompts - Always use --no-interactive to prevent hanging
  1. Generators are starting points - Modify the output as needed to fully satisfy the user's requirements
  1. Verify changes work - Don't just generate; ensure the code builds, lints, and tests pass
  1. Be proactive about fixes - Don't just report errors; attempt to resolve them automatically when possible
  1. Match repo patterns - Study existing similar code in the repo and match its conventions
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.