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:
Use the Nx CLI to discover available generators:
npx nx list @nx/react
npx nx list
This includes:
@nx/react:library, @nx/js:library)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.
Before running any generator, complete these steps:
Use the --help flag to understand all available options:
npx nx g @nx/react:library --help
Pay attention to:
Understanding what the generator actually does helps you:
To find generator source code:
node -e "console.log(require.resolve('@nx/<plugin>/generators.json'));" to find the generators.json, then locate the source from therenode_modules/<plugin>/generators.json
tools/generators/ or a local plugin directory. You can search the repo for the generator name to find it.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.
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.
Ensure all required options have values:
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.
Running with --dry-run first is strongly encouraged but not mandatory. Use your judgment:
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
If the generator fails:
Common failure reasons:
Generators provide a starting point, but the output may need adjustment to match the user's specific requirements:
Run formatting on all generated/modified files:
nx format --fix
Languages other than javascript/typescript might need other formatting invocations too.
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>
When verification fails:
If scope is manageable (a few lint errors, minor type issues):
If issues are extensive (many errors, complex problems):
Local generators first - Always prefer workspace/local generators over external plugin generators when both could work
Understand before running - Read both the schema AND the source code to fully understand what will happen
No prompts - Always use --no-interactive to prevent hanging
Generators are starting points - Modify the output as needed to fully satisfy the user's requirements
Verify changes work - Don't just generate; ensure the code builds, lints, and tests pass
Be proactive about fixes - Don't just report errors; attempt to resolve them automatically when possible
Match repo patterns - Study existing similar code in the repo and match its conventions