Nx单仓库
nx-monorepo
by giuseppe-trisciuoglio
面向 Nx monorepo 开发,涵盖工作区初始化、应用和库生成、affected 命令、缓存与 CI/CD 配置,以及 Module Federation、NestJS 集成。
安装
claude skill add --url github.com/giuseppe-trisciuoglio/developer-kit/tree/main/plugins/developer-kit-typescript/skills/nx-monorepo文档
Nx Monorepo
Overview
Provides comprehensive guidance for working with Nx monorepos in TypeScript/JavaScript projects. Nx is a smart build system with advanced caching, affected command execution, and powerful generators for React, Next.js, NestJS, and more. This skill covers workspace creation, project generation, task execution, caching strategies, Module Federation, and CI/CD integration.
When to Use
Use this skill when:
- Creating a new Nx workspace or initializing Nx in an existing project
- Generating applications, libraries, or components with Nx generators
- Running affected commands or executing tasks across multiple projects
- Setting up CI/CD pipelines for Nx projects (GitHub Actions, CircleCI, etc.)
- Configuring Module Federation with React or Next.js
- Implementing NestJS backend applications within Nx
- Managing TypeScript package libraries with buildable and publishable libs
- Setting up remote caching or Nx Cloud
- Optimizing monorepo build times and caching strategies
- Debugging dependency graph issues or circular dependencies
Trigger phrases: "create Nx workspace", "Nx monorepo", "generate Nx app", "Nx affected", "Nx CI/CD", "Module Federation Nx", "Nx Cloud"
Instructions
Workspace Creation
-
Create a new workspace with interactive setup:
bashnpx create-nx-workspace@latestFollow prompts to select preset (Integrated, Standalone, Package-based) and framework stack.
-
Initialize Nx in an existing project:
bashnx@latest init -
Create with specific preset (non-interactive):
bashnpx create-nx-workspace@latest my-workspace --preset=react
Project Generation
-
Generate a React application:
bashnx g @nx/react:app my-app -
Generate a library:
bash# React library nx g @nx/react:lib my-lib # TypeScript library nx g @nx/js:lib my-util -
Generate a component in lib:
bashnx g @nx/react:component my-comp --project=my-lib -
Generate NestJS backend:
bashnx g @nx/nest:app my-api
Task Execution
-
Run tasks for affected projects only:
bashnx affected -t lint test build -
Run tasks across all projects:
bash# Build all projects nx run-many -t build # Test specific projects nx run-many -t test -p=my-app,my-lib # Test by pattern nx run-many -t test --projects=*-app -
Run specific target on single project:
bashnx run my-app:build -
Visualize dependency graph:
bashnx graph
Project Configuration
Each project has a project.json defining targets, executor, and configurations:
{
"name": "my-app",
"projectType": "application",
"sourceRoot": "apps/my-app/src",
"targets": {
"build": {
"executor": "@nx/react:webpack",
"outputs": ["{workspaceRoot}/dist/apps/my-app"],
"configurations": {
"production": {
"optimization": true
}
}
},
"test": {
"executor": "@nx/vite:test"
}
},
"tags": ["type:app", "scope:frontend"]
}
Dependency Management
-
Set up project dependencies:
json{ "targets": { "build": { "dependsOn": [ { "projects": ["shared-ui"], "target": "build" } ] } } } -
Use tags for organization:
json{ "tags": ["type:ui", "scope:frontend", "platform:web"] }
Module Federation (Nx 17+)
-
Generate a remote (micro-frontend):
bashnx g @nx/react:remote checkout --host=dashboard -
Generate a host:
bashnx g @nx/react:host dashboard
CI/CD Setup
Use affected commands in CI to only build/test changed projects:
# .github/workflows/ci.yml
- run: npx nx affected -t lint --parallel
- run: npx nx affected -t test --parallel
- run: npx nx affected -t build --parallel
Examples
Example 1: Create New React Workspace
Input: "Create a new Nx workspace with React and TypeScript"
Steps:
npx create-nx-workspace@latest my-workspace
# Select: Integrated Monorepo → React → Integrated monorepo (Nx Cloud)
Expected Result: Workspace created with:
apps/directory with React applibs/directory for shared librariesnx.jsonwith cache configuration- CI/CD workflow files ready
Example 2: Run Tests for Changed Projects
Input: "Run tests only for projects affected by recent changes"
Command:
nx affected -t test --base=main~1 --head=main
Expected Result: Only tests for projects affected by changes between commits are executed, leveraging cached results from previous runs.
Example 3: Generate and Build a Shared Library
Input: "Create a shared UI library and use it in the app"
Steps:
# Generate library
nx g @nx/react:lib shared-ui
# Generate component in library
nx g @nx/react:component button --project=shared-ui
# Import in app (tsconfig paths auto-configured)
import { Button } from '@my-workspace/shared-ui'
Expected Result: Buildable library at libs/shared-ui with proper TypeScript path mapping configured.
Example 4: Set Up Module Federation
Input: "Configure Module Federation for micro-frontends"
Steps:
# Create host app
nx g @nx/react:host dashboard
# Add remote to host
nx g @nx/react:remote product-catalog --host=dashboard
# Start dev servers
nx run dashboard:serve
nx run product-catalog:serve
Expected Result: Two separate applications running where product-catalog loads dynamically into dashboard at runtime.
Example 5: Debug Build Dependencies
Input: "Why is my app rebuilding when unrelated lib changes?"
Diagnosis:
# Show project graph
nx graph --focused=my-app
# Check implicit dependencies
nx show project my-app --json | grep implicitDependencies
Solution: Add explicit dependency configuration or use namedInputs in nx.json to exclude certain files from triggering builds.
Best Practices
- Always use
nx affectedin CI to only test/build changed projects - Organize libs by domain/business capability, not by technical layer
- Use tags consistently (
type:app|lib,scope:frontend|backend|shared) - Prevent circular dependencies by configuring
workspaceLayoutboundaries innx.json - Enable remote caching with Nx Cloud for team productivity
- Keep project.json simple - use defaults from
nx.jsonwhen possible - Leverage generators instead of manual file creation for consistency
- Configure
namedInputsto exclude test files from production cache keys - Use Module Federation for independent deployment of micro-frontends
- Keep workspace generators in
tools/for project-specific scaffolding
Constraints and Warnings
- Node.js 18.10+ is required for Nx 17+
- Windows users: Use WSL or Git Bash for best experience
- First-time setup may take longer due to package installation
- Large monorepos (50+ projects) should use distributed task execution
- Module Federation requires webpack 5+ and specific Nx configuration
- Some generators require additional plugins to be installed first
- Cache location: Default
~/.nx/cachecan grow large; configurecacheDirectoryinnx.jsonif needed - Circular dependencies will cause build failures; use
nx graphto visualize - Preset migration: Converting between Integrated/Standalone/Package-based requires manual effort
Reference Files
For detailed guidance on specific topics, consult:
| Topic | Reference File |
|---|---|
| Workspace setup, basic commands | references/basics.md |
| Generators (app, lib, component) | references/generators.md |
| React, Next.js, Expo patterns | references/react.md |
| NestJS backend patterns | references/nestjs.md |
| TypeScript packages | references/typescript.md |
| CI/CD (GitHub, CircleCI, etc.) | references/ci-cd.md |
| Caching, affected, advanced | references/advanced.md |
相关 Skills
网页构建器
by anthropics
面向复杂 claude.ai HTML artifact 开发,快速初始化 React + Tailwind CSS + shadcn/ui 项目并打包为单文件 HTML,适合需要状态管理、路由或多组件交互的页面。
✎ 在 claude.ai 里做复杂网页 Artifact 很省心,多组件、状态和路由都能顺手搭起来,React、Tailwind 与 shadcn/ui 组合效率高、成品也更精致。
前端设计
by anthropics
面向组件、页面、海报和 Web 应用开发,按鲜明视觉方向生成可直接落地的前端代码与高质感 UI,适合做 landing page、Dashboard 或美化现有界面,避开千篇一律的 AI 审美。
✎ 想把页面做得既能上线又有设计感,就用前端设计:组件到整站都能产出,难得的是能避开千篇一律的 AI 味。
网页应用测试
by anthropics
用 Playwright 为本地 Web 应用编写自动化测试,支持启动开发服务器、校验前端交互、排查 UI 异常、抓取截图与浏览器日志,适合调试动态页面和回归验证。
✎ 借助 Playwright 一站式验证本地 Web 应用前端功能,调 UI 时还能同步查看日志和截图,定位问题更快。
相关 MCP 服务
GitHub
编辑精选by GitHub
GitHub 是 MCP 官方参考服务器,让 Claude 直接读写你的代码仓库和 Issues。
✎ 这个参考服务器解决了开发者想让 AI 安全访问 GitHub 数据的问题,适合需要自动化代码审查或 Issue 管理的团队。但注意它只是参考实现,生产环境得自己加固安全。
Context7 文档查询
编辑精选by Context7
Context7 是实时拉取最新文档和代码示例的智能助手,让你告别过时资料。
✎ 它能解决开发者查找文档时信息滞后的问题,特别适合快速上手新库或跟进更新。不过,依赖外部源可能导致偶尔的数据延迟,建议结合官方文档使用。
by tldraw
tldraw 是让 AI 助手直接在无限画布上绘图和协作的 MCP 服务器。
✎ 这解决了 AI 只能输出文本、无法视觉化协作的痛点——想象让 Claude 帮你画流程图或白板讨论。最适合需要快速原型设计或头脑风暴的开发者。不过,目前它只是个基础连接器,你得自己搭建画布应用才能发挥全部潜力。