npx skills add https://github.com/nrwl/nx-ai-agents-config --skill nx-generateNx 生成器是强大的工具,用于在单体仓库中搭建项目、进行自动化代码迁移或自动化重复任务。它们确保整个代码库的一致性并减少样板工作。
当用户想要执行以下操作时,适用此技能:
--no-interactive - 防止出现导致执行挂起的提示使用 Nx CLI 来发现可用的生成器:
npx nx list @nx/reactnpx nx list这包括插件生成器(例如 @nx/react:library)和本地工作区生成器。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
确定哪个(些)生成器可以满足用户的需求。考虑他们想要的产物类型、相关的框架以及提到的任何特定生成器名称。
重要提示:当本地工作区生成器和外部插件生成器都可以满足请求时,应始终优先选择本地工作区生成器。本地生成器是为特定仓库的模式定制的。
如果没有合适的生成器存在,你可以停止使用此技能。然而,需要充分的理由——在决定没有适用的生成器之前,请仔细考虑所有可用的生成器。
使用 --help 标志来了解可用选项:
npx nx g @nx/react:library --help
注意必需的选项、可能需要覆盖的默认值以及与用户请求相关的选项。
除非有特定原因需要可构建,默认使用不可构建的库。
| 类型 | 何时使用 | 生成器标志 |
|---|---|---|
| 不可构建(默认) | 供应用程序使用的内部单体仓库库 | 无 --bundler 标志 |
| 可构建 | 发布到 npm、跨仓库共享、用于缓存命中的稳定库 | --bundler=vite 或 --bundler=swc |
不可构建的库:
.ts/.tsx 源代码可构建的库:
如果不确定,询问用户:"这个库应该是可构建的(有自己的构建步骤,缓存更好)还是不可构建的(直接消费源代码,设置更简单)?"
此步骤至关重要。 仅凭模式定义并不能告诉你一切。阅读源代码有助于你:
查找生成器源代码:
node -e "console.log(require.resolve('@nx/<plugin>/generators.json'));" 找到 generators.json,然后从中定位源代码node_modules/<plugin>/generators.json 读取tools/generators/ 或本地插件目录中。在仓库中搜索生成器名称。阅读源代码后,重新考虑:这是正确的生成器吗?如果不是,请返回步骤 2。
⚠️
--directory标志的行为可能具有误导性。 它应指定生成的库或组件的完整路径,而不是它将被生成到的父路径。# ✅ 正确 - directory 是库的完整路径 nx g @nx/react:library --directory=libs/my-lib # 生成 libs/my-lib/package.json 等文件 # ❌ 错误 - 这将在 libs 和 libs/src/... 创建文件 nx g @nx/react:library --name=my-lib --directory=libs # 生成 libs/package.json 等文件
在生成之前,检查代码库的目标区域:
始终先使用 --dry-run 运行,以验证文件将在正确的位置创建:
npx nx g @nx/react:library --name=my-lib --dry-run --no-interactive
仔细查看输出。如果文件将在错误的位置创建,请根据从生成器源代码中学到的知识调整你的选项。
注意:某些生成器不支持干运行(例如,如果它们安装 npm 包)。如果因此导致干运行失败,则继续实际运行生成器。
执行生成器:
nx generate <generator-name> <options> --no-interactive
提示: 新包通常需要连接工作区依赖项(例如,导入共享类型、被应用程序消费)。
link-workspace-packages技能可以帮助正确添加这些依赖。
生成器提供了一个起点。根据需要修改输出以:
重要提示: 如果你替换或删除生成的测试文件(例如 *.spec.ts),要么编写有意义的替代测试,要么从项目配置中移除 test 目标。空的测试套件将导致 nx test 失败。
格式化所有生成/修改的文件:
nx format --fix
此示例用于内置的 nx 格式化工具 prettier。此工作区可能还有其他格式化工具,请酌情使用。
然后验证生成的代码是否正常工作。请记住,使用生成器所做的更改或后续修改可能会影响各种项目,因此通常仅运行你刚刚创建的产物的目标是不够的。
# 这些目标只是示例!
nx run-many -t build,lint,test,typecheck
这些目标是许多工作区中常见的示例。你应该研究此工作区及其项目可用的其他目标。CI 配置通常是指导哪些关键目标必须通过的很好参考。
如果验证失败但问题可以管理(少量 lint 错误、次要类型问题),请修复它们。如果问题很广泛,先尝试明显的修复,然后向用户上报详细信息,包括生成了什么、什么失败了以及你尝试了什么。
每周安装量
565
仓库
GitHub 星标数
12
首次出现
2026年1月27日
安全审计
安装于
github-copilot545
codex544
gemini-cli541
opencode541
amp536
kimi-cli536
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:
--no-interactive - Prevents prompts that would hang executionUse the Nx CLI to discover available generators:
npx nx list @nx/reactnpx nx listThis includes plugin generators (e.g., @nx/react:library) and local workspace generators.
Identify which generator(s) could fulfill the user's needs. Consider what artifact type they want, which framework is relevant, and any specific generator names mentioned.
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.
If no suitable generator exists, you can stop using this skill. However, the burden of proof is high—carefully consider all available generators before deciding none apply.
Use the --help flag to understand available options:
npx nx g @nx/react:library --help
Pay attention to required options, defaults that might need overriding, and options relevant to the user's request.
Default to non-buildable libraries unless there's a specific reason for buildable.
| Type | When to use | Generator flags |
|---|---|---|
| Non-buildable (default) | Internal monorepo libs consumed by apps | No --bundler flag |
| Buildable | Publishing to npm, cross-repo sharing, stable libs for cache hits | --bundler=vite or --bundler=swc |
Non-buildable libs:
.ts/.tsx source directlyBuildable libs:
If unclear, ask the user: "Should this library be buildable (own build step, better caching) or non-buildable (source consumed directly, simpler setup)?"
This step is critical. The schema alone does not tell you everything. Reading the source code 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.jsontools/generators/ or a local plugin directory. Search the repo for the generator name.After reading the source, reconsider: Is this the right generator? If not, go back to step 2.
⚠️
--directoryflag behavior can be misleading. It should specify the full path of the generated library or component, not the parent path that it will be generated in.# ✅ Correct - directory is the full path for the library nx g @nx/react:library --directory=libs/my-lib # generates libs/my-lib/package.json and more # ❌ Wrong - this will create files at libs and libs/src/... nx g @nx/react:library --name=my-lib --directory=libs # generates libs/package.json and more
Before generating, examine the target area of the codebase:
Always run with--dry-run first to verify files will be created in the correct location:
npx nx g @nx/react:library --name=my-lib --dry-run --no-interactive
Review the output carefully. If files would be created in the wrong location, adjust your options based on what you learned from the generator source code.
Note: Some generators don't support dry-run (e.g., if they install npm packages). If dry-run fails for this reason, proceed to running the generator for real.
Execute the generator:
nx generate <generator-name> <options> --no-interactive
Tip: New packages often need workspace dependencies wired up (e.g., importing shared types, being consumed by apps). The
link-workspace-packagesskill can help add these correctly.
Generators provide a starting point. Modify the output as needed to:
Important: If you replace or delete generated test files (e.g., *.spec.ts), either write meaningful replacement tests or remove the test target from the project configuration. Empty test suites will cause nx test to fail.
Format all generated/modified files:
nx format --fix
This example is for built-in nx formatting with prettier. There might be other formatting tools for this workspace, use these when appropriate.
Then verify the generated code works. Keep in mind that the changes you make with a generator or subsequent modifications might impact various projects so it's usually not enough to only run targets for the artifact you just created.
# these targets are just an example!
nx run-many -t build,lint,test,typecheck
These targets are common examples used across many workspaces. You should do research into other targets available for this workspace and its projects. CI configuration is usually a good guide for what the critical targets are that have to pass.
If verification fails with manageable issues (a few lint errors, minor type issues), fix them. If issues are extensive, attempt obvious fixes first, then escalate to the user with details about what was generated, what's failing, and what you've attempted.
Weekly Installs
565
Repository
GitHub Stars
12
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubWarnSocketPassSnykPass
Installed on
github-copilot545
codex544
gemini-cli541
opencode541
amp536
kimi-cli536
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装
OpenAPI 转 TypeScript 工具 - 自动生成 API 接口与类型守卫
563 周安装
数据库模式设计器 - 内置最佳实践,自动生成生产级SQL/NoSQL数据库架构
564 周安装
Rust Unsafe代码检查器 - 安全使用Unsafe Rust的完整指南与最佳实践
564 周安装
.NET并发编程模式指南:async/await、Channels、Akka.NET选择决策树
565 周安装
韩语语法检查器 - 基于国立国语院标准的拼写、空格、语法、标点错误检测与纠正
565 周安装
技能安全扫描器 - 检测Claude技能安全漏洞,防范提示注入与恶意代码
565 周安装