---
roadcrew_template_name: "analyze-package-manager.md"
roadcrew_template_type: "command"
execution_mode: "auto-execute"
roadcrew_template_version: "v1.0"
roadcrew_last_updated: "2025-11-02"
roadcrew_min_version: "1.5.0"
roadcrew_license: "See LICENSE file in .roadcrew folder"
roadcrew_copyright: "Copyright (c) 2025 North Star Holdings, LLC"
spdx_license_identifier: "LicenseRef-RoadcrewLicense-1.0"
---

# Analyze Package Manager & Dependency Efficiency

Comprehensive analysis of package manager (npm, pnpm, yarn) efficiency, monorepo readiness, cost optimization opportunities, and CI/CD performance improvements.

## What This Command Does

1. **Detects current package manager** (npm, pnpm, yarn)
2. **Comparative analysis:**
   - npm: ~60s installs, 70% disk waste, basic monorepo support
   - pnpm: ~20s installs, 70% disk savings, native monorepo support
   - yarn: ~40s installs, balanced approach, workspaces support
3. **Evaluates monorepo setup** (if applicable)
   - Workspace configuration validation
   - Package structure analysis
   - Dependency graph health
4. **Calculates cost savings** from optimization/migration paths
5. **Recommends improvements** tailored to current package manager:
   - **npm:** Caching, deduplication, CI filtering
   - **pnpm:** Monorepo optimization, store caching, workspace filtering
   - **yarn:** Workspace optimization, PnP mode benefits

## Usage

Run this command when:
- Evaluating package manager choice (npm vs pnpm vs yarn)
- Planning monorepo adoption/migration
- Optimizing CI/CD efficiency and cost
- Assessing disk space and install time improvements
- Comparing dependency management strategies

## Parameters

### `--repo` (optional)

Specify an external repository path to analyze:

```bash
# Analyze current directory (default)
/analyze-package-manager

# Analyze external repository
/analyze-package-manager --repo=/path/to/external/repo
```

### `--focus` (optional)

Focus analysis on specific area:

```bash
# Analyze all areas (default)
/analyze-package-manager --focus=all

# Compare package managers (npm vs pnpm vs yarn)
/analyze-package-manager --focus=comparison

# Evaluate monorepo readiness
/analyze-package-manager --focus=monorepo

# Focus on specific package manager optimizations
/analyze-package-manager --focus=current    # Optimize current PM
/analyze-package-manager --focus=npm        # npm-specific tips
/analyze-package-manager --focus=pnpm       # pnpm-specific tips
/analyze-package-manager --focus=yarn       # yarn-specific tips

# Focus on CI/CD optimizations
/analyze-package-manager --focus=cicd

# Focus on cost savings potential
/analyze-package-manager --focus=savings
```

## Process

### 1. Compile Detection Utilities

```bash
npm run ensure-built
```

### 2. Import and Run Analysis

```typescript
import { analyzePackageManager } from './dist/scripts/utils/roadcrew/analyze-package-manager.js';

const repoPath = args.repo || process.cwd();
const report = analyzePackageManager(repoPath);
```

### 3. Analyze Each Section

#### A. Package Manager Detection & Comparison

Detects: `package.json`, `package-lock.json`, `pnpm-lock.yaml`, `yarn.lock`

```
📦 Package Manager Analysis
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Current: npm v10.2.3 (package-lock.json detected)

Comparison Metrics:
┌─────────────────┬──────────┬──────────┬──────────┐
│ Metric          │ npm      │ pnpm     │ yarn     │
├─────────────────┼──────────┼──────────┼──────────┤
│ Install Time    │ 60s      │ 20s      │ 40s      │
│ Disk Space      │ 850 MB   │ 255 MB   │ 480 MB   │
│ Parallelization │ Limited  │ Full     │ Good     │
│ Monorepo        │ Partial  │ Native   │ Native   │
│ Annual CI Cost  │ $6,000   │ $1,200   │ $2,400   │
└─────────────────┴──────────┴──────────┴──────────┘

Your best opportunities (for npm):
  • Dependency caching: +30% speed
  • Deduplication: -20% disk
  • Filtered CI: -40% time
  • Potential savings: $2,400/year
```

#### B. Monorepo Configuration (if applicable)

```
📂 Monorepo Configuration
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Status: ✅ Monorepo detected
Type: npm workspaces (4 packages)

Workspaces:
  1. @myorg/frontend     - 2,341 files, 45 MB
  2. @myorg/backend      - 1,892 files, 32 MB
  3. @myorg/shared       - 456 files, 8 MB
  4. @myorg/cli          - 234 files, 4 MB

Health Check:
  ✅ All workspaces have package.json
  ✅ No name collisions detected
  ⚠️  Backend has no devDependencies
  ⚠️  Consider adding shared dependencies to root

Optimization tips for npm workspaces:
  • Use --workspace filter in scripts
  • Cache per-workspace in CI
  • Consider pnpm for 70% disk savings
```

#### C. Disk Space Analysis

```
💾 Disk Space Efficiency
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Current State (npm):
  node_modules: 850 MB
  Duplicates:   298 MB (35% waste)

Quick Wins (npm):
  • npm dedup: reduces to ~750 MB (-12%)
  • npm ci in CI: reduces to ~700 MB (-18%)

With pnpm:
  .pnpm-store:  255 MB (content-addressed)
  Symlinks:     <1 MB (per workspace)
  Total Saved:  595 MB (70% reduction)

Annual Impact:
  CI Runner Disk:    -200 GB ⚡
  Local Dev Disk:    -600 MB per machine
  GitHub Storage:    -$150/year
```

#### D. Install Performance

```
⚡ Install Performance
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Dependency Count: 450 (direct + transitive)

npm (current):
  Time: ~60 seconds
  Optimizations available:
    • npm ci (vs install): -10s ✓
    • Frozen lockfile: -5s ✓
    • Cache in CI: -20s ✓
  Achievable: ~25 seconds (58% improvement)

pnpm (alternative):
  Time: ~20 seconds
  Monorepo-optimized: ~12s (80% improvement)

yarn (alternative):
  Time: ~40 seconds
  Workspaces optimized: ~18s (70% improvement)

CI/CD Impact (100 runs/month):
  npm (current):     100 × 60s = 6,000 min/year = $48/year
  npm (optimized):   100 × 25s = 2,500 min/year = $20/year
  pnpm:              100 × 20s = 2,000 min/year = $16/year
  Savings (npm opt): $28/year
  Savings (pnpm):    $32/year
```

#### E. Package Manager Specific Recommendations

**NPM Path:**
```
🔧 npm Optimization Opportunities
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

TIER 1: Easy Wins (Implement First - 30 min total)
┌──────────────────────────────────────────────────┐
│ 1. Use npm ci in CI (vs npm install)             │
│    Effort: ⏱️  5 min                             │
│    Savings: 10s per run (10% speed)             │
│    Annual Savings: $24                           │
│    Steps:                                        │
│    - Replace 'npm install' with 'npm ci'        │
│    - Requires package-lock.json (already there) │
│    - Exact reproducible installs                │
└──────────────────────────────────────────────────┘

│ 2. Enable npm cache in CI                        │
│    Effort: ⏱️  10 min                            │
│    Savings: 20s per run (33% speed)            │
│    Annual Savings: $48                           │
│    Steps:                                        │
│    - Add actions/cache@v3 (GitHub Actions)      │
│    - Cache: ~/.npm                              │
│    - Key: npm-cache-${{ hashFiles(...) }}      │
│    - Works with npm ci automatically            │
└──────────────────────────────────────────────────┘

│ 3. Remove unused dependencies                    │
│    Effort: ⏱️  15 min                            │
│    Savings: 5s per run + 50 MB disk            │
│    Annual Savings: $12                           │
│    Steps:                                        │
│    - npm ls (identify unused)                   │
│    - npm uninstall <package>                    │
│    - npm prune                                  │
│    - Reduces node_modules bloat                 │
└──────────────────────────────────────────────────┘

TIER 2: Medium Effort (30 min - 2 hours)
┌──────────────────────────────────────────────────┐
│ 4. Implement workspace-aware CI                  │
│    Effort: ⏱️  1-2 hours                        │
│    Savings: 30s per run (50% CI time)          │
│    Annual Savings: $72                           │
│    Steps:                                        │
│    - npm ls -r (detect monorepo)               │
│    - npm workspaces list                        │
│    - Filter: npm --workspace=<name> test       │
│    - Cache per workspace                        │
│    - Skip unchanged workspaces                  │
└──────────────────────────────────────────────────┘

│ 5. npm dedupe optimization                       │
│    Effort: ⏱️  30 min                            │
│    Savings: 15% disk, -5s CI                   │
│    Annual Savings: $36                           │
│    Steps:                                        │
│    - npm dedupe                                 │
│    - Removes duplicate deps                    │
│    - Reduces from 850 MB → ~750 MB             │
│    - Re-run after major dependency adds        │
└──────────────────────────────────────────────────┘

TIER 3: Migration Path (4-6 hours)
┌──────────────────────────────────────────────────┐
│ 6. Migrate to pnpm (if at scale)                 │
│    Effort: ⏱️  4-6 hours                        │
│    Savings: 60% faster CI, 70% disk            │
│    Annual Savings: $288 (+ developer time)     │
│    Steps:                                        │
│    - npm i -g pnpm@latest                      │
│    - pnpm install (generates pnpm-lock.yaml)  │
│    - Update CI workflows                        │
│    - Test locally + staging first               │
│    - Benefits especially for monorepo           │
└──────────────────────────────────────────────────┘

Total npm optimization potential: ~$180/year
```

**PNPM Path:**
```
🔧 pnpm Optimization Opportunities
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

TIER 1: Easy Wins (15 min)
┌──────────────────────────────────────────────────┐
│ 1. Add pnpm store caching to CI                  │
│    Effort: ⏱️  Easy (15 min)                    │
│    Savings: 10s per run (33% speed)           │
│    Annual Savings: $24                          │
│    Implementation:                              │
│    - Add actions/cache@v3                      │
│    - Cache: ~/.pnpm-store                      │
│    - Key: pnpm-store-${{ hashFiles(...) }}    │
└──────────────────────────────────────────────────┘

TIER 2: Medium Effort (2-4 hours)
┌──────────────────────────────────────────────────┐
│ 2. Use pnpm workspace filtering                  │
│    Effort: ⏱️  Medium (2-4 hours)              │
│    Savings: 40% CI time per build              │
│    Annual Savings: $96                          │
│    Implementation:                              │
│    - Detect changed packages: git diff          │
│    - Run: pnpm --filter=[CHANGED] install      │
│    - Cache per workspace                        │
│    - Skip unchanged workspaces                  │
└──────────────────────────────────────────────────┘

TIER 3: Hard (Strategic - 1-2 weeks)
┌──────────────────────────────────────────────────┐
│ 3. Workspace-scoped CI/CD matrix                 │
│    Effort: ⏱️  Hard (1-2 weeks)                │
│    Savings: 50% CI time                        │
│    Annual Savings: $144                        │
│    Implementation:                              │
│    - Extract workspace matrix from pnpm       │
│    - Create per-workspace test matrices        │
│    - Parallel execution                        │
│    - Shared build cache                        │
└──────────────────────────────────────────────────┘

Total pnpm optimization potential: ~$264/year
```

**Yarn Path:**
```
🔧 yarn Optimization Opportunities
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

TIER 1: Easy Wins (20 min)
┌──────────────────────────────────────────────────┐
│ 1. Enable yarn cache in CI                       │
│    Effort: ⏱️  Easy (10 min)                    │
│    Savings: 15s per run (37% speed)           │
│    Annual Savings: $36                          │
│    Implementation:                              │
│    - Add actions/cache@v3                      │
│    - Cache: ~/.yarn/cache                      │
│    - Key: yarn-cache-${{ hashFiles(...) }}    │
└──────────────────────────────────────────────────┘

│ 2. Use yarn workspaces filtering                 │
│    Effort: ⏱️  Easy (10 min)                    │
│    Savings: 20% CI time                        │
│    Annual Savings: $48                          │
│    Implementation:                              │
│    - yarn workspaces list                      │
│    - yarn workspace <name> run test            │
│    - Per-workspace caching                     │
│    - Only rebuild changed                      │
└──────────────────────────────────────────────────┘

TIER 2: Medium Effort (2-3 hours)
┌──────────────────────────────────────────────────┐
│ 3. Consider yarn PnP (Plug'n'Play) mode          │
│    Effort: ⏱️  Medium (2-3 hours)              │
│    Savings: 30% install time, -60% disk       │
│    Annual Savings: $60                          │
│    Implementation:                              │
│    - Enable: nodeLinker: pnp                   │
│    - Test compatibility first                  │
│    - May need adjustments for some packages    │
│    - Significant perf gains                    │
└──────────────────────────────────────────────────┘

Total yarn optimization potential: ~$144/year
```

### 4. Calculate Total Savings

```
💰 Total Cost/Efficiency Analysis
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

For npm teams (current state):
  Baseline: 60s install, $6,000 CI cost/year
  After optimization tier 1-2: $5,880 saved
  After migration to pnpm: additional $4,800
  Total potential: $10,680/year

For organizations (20 developers):
  Developer time savings: 20 devs × 5 min/day × 250 days = 416 hours/year
  Economic value @ $100/hr: $41,600
  CI savings: $10,680
  Total value: $52,280/year

Recommendation:
  ✅ Start with Tier 1 (easy wins, 30 min) → $24/year saved + quick wins
  ✅ Move to Tier 2 (2 hours) → $96/year additional
  ✅ Consider pnpm migration for monorepos → max savings
```

### 5. Generate Report

Create timestamped report in `docs/reports/` with filename `dependencies-YYYY-MM-DD-HHMMSS.md`:

**Filename:** `package-manager-analysis-YYYY-MM-DD-HHMMSS.md`

**Includes:**
- Package manager detection and comparison
- Current monorepo configuration status (if applicable)
- Disk space efficiency analysis
- Install performance metrics
- PM-specific optimization opportunities (prioritized by effort)
- Cost/benefit analysis
- Migration roadmap (if applicable)
- Actionable next steps with time estimates

## Example Output

```markdown
# Package Manager Analysis - 2025-11-02 10:30:00

## Executive Summary

**Current:** npm v10.2.3 (good baseline, optimization opportunities available)

**Key Findings:**
- Install time: 60s (can reduce to 25s with tier 1-2)
- Disk space: 850 MB (can reduce to 750 MB)
- Potential annual savings: $180/year (optimization) or $288/year (pnpm migration)

**Recommendations (prioritized):**
1. ✅ Easy: Use npm ci + caching (30 min, +$72/year)
2. ⏱️  Medium: Workspace-aware CI (2 hours, +$72/year)
3. 🚀 Strategic: pnpm migration if monorepo grows (6 hours, +$288/year)

---

## Current State Analysis

| Metric | npm | Potential |
|--------|-----|-----------|
| Install Time | 60s | 25s |
| Disk Space | 850 MB | 750 MB |
| Annual CI Cost | $6,000 | $5,880 (npm opt) or $1,200 (pnpm) |

---

## Actionable Next Steps

1. **This week:** Implement tier 1 (npm ci + cache)
2. **Next sprint:** Workspace filtering
3. **Q2:** Evaluate pnpm if team grows

---

[Full analysis details...]
```

## Error Handling

- If detection utilities fail, show error and continue with estimates
- If lock file is malformed, provide recovery steps
- If monorepo config incomplete, show gaps and recommendations
- If CI/CD workflows not detected, show generic recommendations

## Notes

- Supports npm v7+, pnpm v6+, yarn v1.22+
- All analysis uses actual code inspection, not cached reports
- Savings calculations based on GitHub Actions pricing ($0.008/min)
- Recommend running after every major dependency change
- Works with single-repo and monorepo setups
- Use with `/analyze-repo` for complete tech stack view

## See Also

- `/analyze-repo` - General tech stack detection
- `/analyze-architecture-full` - Complete architecture analysis
- `/minimize-cost` - Additional cost optimization opportunities
- `/analyze-cicd` - CI/CD pipeline optimization
