$npx -y skills add mjunaidca/mjs-agent-skills --skill nx-monorepoNx monorepo management skill for AI-native development. This skill should be used when working with Nx workspaces, project graphs, affected detection, code generation, and caching. Use when: analyzing dependencies, running affected commands, generating code, configuring Nx Cloud,
| 1 | # Nx Monorepo |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | This skill provides expert-level capabilities for Nx monorepo management. Nx is the standard build orchestrator for this AI-native platform due to its official MCP server integration. |
| 6 | |
| 7 | **Why Nx**: Official MCP server, TypeScript-native, 5-minute setup, auto-generates CLAUDE.md/AGENTS.md for AI assistants. |
| 8 | |
| 9 | ## MCP Tools Available |
| 10 | |
| 11 | ``` |
| 12 | nx_docs - Query Nx documentation |
| 13 | nx_available_plugins - List official Nx plugins (NOT installed by default) |
| 14 | ``` |
| 15 | |
| 16 | **Key Insight**: MCP provides documentation lookup. Use **Nx CLI** for all operations. |
| 17 | |
| 18 | ## Core CLI Commands |
| 19 | |
| 20 | ### Project Graph Analysis |
| 21 | |
| 22 | ```bash |
| 23 | # View interactive project graph |
| 24 | nx graph |
| 25 | |
| 26 | # JSON output for programmatic use |
| 27 | nx graph --file=output.json |
| 28 | |
| 29 | # Show dependencies of specific project |
| 30 | nx graph --focus=my-app |
| 31 | |
| 32 | # Show what depends on a project |
| 33 | nx graph --affected |
| 34 | ``` |
| 35 | |
| 36 | ### Affected Detection |
| 37 | |
| 38 | ```bash |
| 39 | # What's affected since main? |
| 40 | nx affected -t build |
| 41 | nx affected -t test |
| 42 | nx affected -t lint |
| 43 | |
| 44 | # Compare against specific base |
| 45 | nx affected -t build --base=origin/main --head=HEAD |
| 46 | |
| 47 | # Show affected projects only |
| 48 | nx show projects --affected |
| 49 | ``` |
| 50 | |
| 51 | ### Running Tasks |
| 52 | |
| 53 | ```bash |
| 54 | # Run task for all projects |
| 55 | nx run-many -t build |
| 56 | nx run-many -t test |
| 57 | |
| 58 | # Run for specific projects |
| 59 | nx run-many -t build --projects=app-a,lib-b |
| 60 | |
| 61 | # Run with parallelism control |
| 62 | nx run-many -t build --parallel=4 |
| 63 | |
| 64 | # Single project |
| 65 | nx build my-app |
| 66 | nx test my-lib |
| 67 | ``` |
| 68 | |
| 69 | ### Code Generation |
| 70 | |
| 71 | ```bash |
| 72 | # List available generators |
| 73 | nx list @nx/next |
| 74 | nx list @nx/react |
| 75 | |
| 76 | # Generate new application |
| 77 | nx g @nx/next:app my-app |
| 78 | nx g @nx/react:app my-frontend |
| 79 | |
| 80 | # Generate library |
| 81 | nx g @nx/js:lib shared-utils |
| 82 | nx g @nx/react:lib ui-components |
| 83 | |
| 84 | # Dry run (preview) |
| 85 | nx g @nx/next:app my-app --dry-run |
| 86 | ``` |
| 87 | |
| 88 | ## Configuration Files |
| 89 | |
| 90 | ### nx.json (Workspace Config) |
| 91 | |
| 92 | ```json |
| 93 | { |
| 94 | "targetDefaults": { |
| 95 | "build": { |
| 96 | "dependsOn": ["^build"], |
| 97 | "cache": true |
| 98 | }, |
| 99 | "test": { |
| 100 | "cache": true |
| 101 | }, |
| 102 | "lint": { |
| 103 | "cache": true |
| 104 | } |
| 105 | }, |
| 106 | "namedInputs": { |
| 107 | "default": ["{projectRoot}/**/*"], |
| 108 | "production": ["default", "!{projectRoot}/**/*.spec.ts"] |
| 109 | }, |
| 110 | "defaultBase": "main" |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | ### project.json (Project Config) |
| 115 | |
| 116 | ```json |
| 117 | { |
| 118 | "name": "my-app", |
| 119 | "projectType": "application", |
| 120 | "targets": { |
| 121 | "build": { |
| 122 | "executor": "@nx/next:build", |
| 123 | "outputs": ["{options.outputPath}"], |
| 124 | "options": { |
| 125 | "outputPath": "dist/apps/my-app" |
| 126 | } |
| 127 | }, |
| 128 | "serve": { |
| 129 | "executor": "@nx/next:server", |
| 130 | "options": { |
| 131 | "buildTarget": "my-app:build" |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | ``` |
| 137 | |
| 138 | ## Caching |
| 139 | |
| 140 | ### Local Cache |
| 141 | |
| 142 | ```bash |
| 143 | # Cache is automatic for cacheable targets |
| 144 | nx build my-app # First run: executes |
| 145 | nx build my-app # Second run: cached (instant) |
| 146 | |
| 147 | # Clear cache |
| 148 | nx reset |
| 149 | ``` |
| 150 | |
| 151 | ### Nx Cloud (Remote Cache) |
| 152 | |
| 153 | ```bash |
| 154 | # Connect to Nx Cloud |
| 155 | npx nx connect |
| 156 | |
| 157 | # Or add manually |
| 158 | nx g @nx/workspace:ci-workflow |
| 159 | |
| 160 | # Verify connection |
| 161 | nx run-many -t build |
| 162 | # Look for: "Remote cache hit" |
| 163 | ``` |
| 164 | |
| 165 | ### Cache Inputs |
| 166 | |
| 167 | ```json |
| 168 | // In project.json or nx.json |
| 169 | { |
| 170 | "targets": { |
| 171 | "build": { |
| 172 | "inputs": [ |
| 173 | "default", |
| 174 | "^production", |
| 175 | { "externalDependencies": ["next"] } |
| 176 | ] |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | ``` |
| 181 | |
| 182 | ## Common Patterns |
| 183 | |
| 184 | ### Adding a New JS/TS App |
| 185 | |
| 186 | ```bash |
| 187 | # 1. Add plugin (if not already installed) |
| 188 | pnpm nx add @nx/next # For Next.js |
| 189 | pnpm nx add @nx/react # For React |
| 190 | pnpm nx add @nx/node # For Node/Express |
| 191 | |
| 192 | # 2. Generate app |
| 193 | pnpm nx g @nx/next:app dashboard --directory=apps/dashboard |
| 194 | |
| 195 | # 3. Verify in graph |
| 196 | pnpm nx graph --focus=dashboard |
| 197 | |
| 198 | # 4. Build & Serve |
| 199 | pnpm nx build dashboard |
| 200 | pnpm nx serve dashboard |
| 201 | ``` |
| 202 | |
| 203 | ### Adding a Python App (uv Workspace) |
| 204 | |
| 205 | Python projects use **uv workspaces** (similar to pnpm workspaces for JS) with manual `project.json` for Nx: |
| 206 | |
| 207 | ```bash |
| 208 | # 1. Create directory and initialize |
| 209 | mkdir -p apps/my-python-app |
| 210 | cd apps/my-python-app |
| 211 | uv init |
| 212 | uv add --group dev pytest ruff mypy |
| 213 | cd ../.. |
| 214 | |
| 215 | # 2. Add to uv workspace (root pyproject.toml) |
| 216 | ``` |
| 217 | |
| 218 | Edit root `pyproject.toml`: |
| 219 | ```toml |
| 220 | [tool.uv.workspace] |
| 221 | members = [ |
| 222 | "apps/panaversity-fs-py", |
| 223 | "apps/my-python-app", # Add new project here |
| 224 | ] |
| 225 | ``` |
| 226 | |
| 227 | ```bash |
| 228 | # 3. Sync all Python deps from root |
| 229 | uv sync --extra dev |
| 230 | ``` |
| 231 | |
| 232 | **apps/my-python-app/project.json** (for Nx): |
| 233 | ```json |
| 234 | { |
| 235 | "name": "my-python-app", |
| 236 | "projectType": "application", |
| 237 | "targets": { |
| 238 | "build": { |
| 239 | "command": "uv build", |
| 240 | "options": { "cwd": "apps/my-python-app" } |
| 241 | }, |
| 242 | "test": { |
| 243 | "command": "uv run --extra dev pytest", |
| 244 | "options": { "cwd": "apps/my-python-app" } |
| 245 | }, |
| 246 | "lint": { |
| 247 | "command": "uv run --extra dev ruff chec |