Skip to content
Back to skills

607 Best Practices 42f76186

ASecurity

Apply meaningful tags to enforce module boundaries:

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added June 1, 2026
developmentgobashangulargitapiperformance

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned June 1, 2026

npx -y skills add tools-only/X-Skills --skill 607-best-practices_42f76186 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 607 Best Practices 42f76186?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 607 Best Practices 42f76186
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-607-best-practices-42f76186/badge)](https://www.skillsdirectory.com/skills/tools-only-607-best-practices-42f76186)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Nx Best Practices

## Do's ✅

### Use Tags Consistently

Apply meaningful tags to enforce module boundaries:

```json
{
  "tags": ["type:feature", "scope:web"]
}
```

Benefits:

- Prevents circular dependencies
- Enforces architectural patterns
- Makes dependency graph clearer

### Enable Caching Early

Configure caching from the start:

```json
{
  "targetDefaults": {
    "build": {
      "cache": true,
      "inputs": ["production", "^production"]
    }
  }
}
```

Benefits:

- 50-80% CI time reduction
- Faster local development
- Better resource utilization

### Keep Libraries Focused

Each library should have single responsibility:

```bash
# Good - Focused libraries
libs/
  ui/
    buttons/
    forms/
  data-access/
    users/
    products/

# Avoid - Mixed concerns
libs/
  shared/  # Too broad
    everything.ts
```

### Use Affected Commands

Always test only what changed:

```bash
# In CI
nx affected -t test --base=main

# Not
nx run-many -t test --all
```

### Document Boundaries

Add README to explain module structure:

```markdown
# Library Architecture

## Type Tags
- `type:feature` - Business logic
- `type:ui` - Components
- `type:data-access` - API calls

## Scope Tags
- `scope:web` - Web app specific
- `scope:mobile` - Mobile app specific
- `scope:shared` - Shared across apps
```

### Always Use `nx show project`

Get full resolved configuration:

```bash
# Correct
nx show project my-app --json

# Wrong - incomplete config
cat apps/my-app/project.json
```

## Don'ts ❌

### Don't Create Circular Dependencies

Circular deps break build order:

```bash
# Bad
lib-a → lib-b → lib-a  # Circular!

# Good
lib-a → lib-b → lib-c  # Acyclic
```

Check with:

```bash
nx graph
```

### Don't Skip Affected in CI

Running all tests wastes resources:

```bash
# Wasteful in CI
nx run-many -t test --all  # ❌

# Efficient
nx affected -t test  # ✅
```

### Don't Ignore Boundary Rules

Boundary violations accumulate technical debt:

```bash
# Fix violations immediately
nx lint --fix

# Don't disable rules
"@nx/enforce-module-boundaries": "off"  # ❌
```

### Don't Over-Granularize

Too many small libraries creates overhead:

```bash
# Too granular
libs/
  utils/
    add/  # Just one function
    subtract/  # Just one function

# Better
libs/
  utils/
    math/  # Related functions together
```

### Don't Read project.json Directly

It contains only partial configuration:

```bash
# Wrong
cat project.json  # ❌

# Correct
nx show project my-app --json  # ✅
```

## Troubleshooting

### Package Manager Not Detected

**Problem**: Nx doesn't detect package manager

**Solution**: Ensure lockfile exists

- npm: `package-lock.json`
- yarn: `yarn.lock`
- pnpm: `pnpm-lock.yaml`

### Targets Not Showing

**Problem**: Targets missing in `nx show projects`

**Cause**: Reading project.json instead of resolved config

**Solution**:

```bash
nx show project <name> --json | jq '.targets'
```

### Affected Commands Not Working

**Problem**: All projects shown as affected

**Causes**:

1. Missing git history
2. Wrong base branch

**Solutions**:

```bash
# In CI: fetch full history
- uses: actions/checkout@v4
  with:
    fetch-depth: 0

# Specify correct base
nx affected -t test --base=origin/main
```

### Cache Not Working

**Problem**: Tasks always re-run

**Causes**:

1. Cache disabled
2. Inputs not configured
3. Cache corrupted

**Solutions**:

```bash
# Check if cache enabled
cat nx.json | jq '.targetDefaults.build.cache'

# Clear cache
nx reset

# Configure inputs
{
  "targetDefaults": {
    "build": {
      "cache": true,
      "inputs": ["production", "^production"]
    }
  }
}
```

### Circular Dependency Errors

**Problem**: Build fails with circular dependency

**Detection**:

```bash
nx graph
```

**Solution**:

1. Find the cycle in dependency graph
2. Extract shared code to new library
3. Update imports to break cycle

### Module Boundary Violations

**Problem**: Import violates boundary rules

**Error**:

```
A project tagged with "scope:web" can only depend on libs tagged with "scope:web", "scope:shared"
```

**Solutions**:

```bash
# Check current tags
nx show project my-lib --json | jq '.tags'

# Update tags in project.json
{
  "tags": ["scope:shared"]  # Allow cross-scope import
}

# Or move code to allowed scope
```

### Build Performance Issues

**Problem**: Builds are slow

**Solutions**:

1. Enable caching:

```json
{
  "targetDefaults": {
    "build": {
      "cache": true
    }
  }
}
```

1. Use affected:

```bash
nx affected -t build --base=main
```

1. Increase parallelization:

```bash
nx affected -t build --parallel=5
```

1. Enable Nx Cloud:

```json
{
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx-cloud"
    }
  }
}
```

### Git Submodule Issues

**Problem**: Affected detection wrong with submodules

**Solution**: Configure affected in nx.json:

```json
{
  "affected": {
    "defaultBase": "main"
  }
}
```

## Performance Optimization

### Enable Remote Caching

Use Nx Cloud or self-hosted cache:

```json
{
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx-cloud",
      "options": {
        "cacheableOperations": ["build", "lint", "test"],
        "accessToken": "your-token"
      }
    }
  }
}
```

### Configure Inputs Properly

Define what affects cache:

```json
{
  "namedInputs": {
    "default": ["{projectRoot}/**/*"],
    "production": [
      "default",
      "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?"
    ]
  }
}
```

### Use Parallel Execution

Leverage multi-core CPUs:

```bash
# Local development
nx affected -t test --parallel=3

# CI with more resources
nx affected -t test --parallel=5
```

### Optimize Bundle Size

Configure production builds:

```json
{
  "configurations": {
    "production": {
      "optimization": true,
      "sourceMap": false,
      "extractLicenses": true,
      "namedChunks": false
    }
  }
}
```

## Migration Guide

### From Lerna to Nx

```bash
# Add Nx to existing Lerna monorepo
npx nx@latest init

# Migrate Lerna commands
lerna run test → nx run-many -t test --all
lerna run test --since → nx affected -t test
```

### From Angular CLI to Nx

```bash
# Add Nx to Angular workspace
npx nx@latest init

# Continue using Angular CLI commands
ng build → nx build
ng serve → nx serve
```

### Updating Nx

```bash
# Check for updates
nx migrate latest

# Review migrations
cat migrations.json

# Run migrations
nx migrate --run-migrations

# Clean up
rm migrations.json
```

Files in this skill

  • README.md973 B
  • skill.md6.3 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.