|
| 1 | +# Package Management Rules |
| 2 | + |
| 3 | +This document defines the rules for managing dependencies in package.json. |
| 4 | + |
| 5 | +## Fixed Versions Only |
| 6 | + |
| 7 | +**All package versions in package.json MUST be exact (fixed) versions.** |
| 8 | + |
| 9 | +### Rules |
| 10 | + |
| 11 | +| Allowed | Not Allowed | |
| 12 | +|---------|-------------| |
| 13 | +| `"express": "4.18.2"` | `"express": "^4.18.2"` | |
| 14 | +| `"typescript": "5.3.3"` | `"typescript": "~5.3.3"` | |
| 15 | +| `"lodash": "4.17.21"` | `"lodash": ">=4.0.0"` | |
| 16 | +| | `"lodash": "4.x"` | |
| 17 | +| | `"lodash": "*"` | |
| 18 | + |
| 19 | +### Why Fixed Versions? |
| 20 | + |
| 21 | +1. **Reproducibility**: Every installation produces identical `node_modules` |
| 22 | +2. **Stability**: No surprise breaking changes from automatic minor/patch updates |
| 23 | +3. **Security Control**: Updates are intentional and reviewed, not automatic |
| 24 | +4. **Debugging**: Easier to identify which version introduced issues |
| 25 | +5. **Framework Responsibility**: As a framework, we must ensure consuming projects get predictable behavior |
| 26 | + |
| 27 | +### When Adding New Packages |
| 28 | + |
| 29 | +```bash |
| 30 | +# CORRECT: Install and immediately fix the version |
| 31 | + |
| 32 | + |
| 33 | +# Then verify package.json has exact version (no ^ or ~) |
| 34 | +``` |
| 35 | + |
| 36 | +If npm adds `^` automatically, manually remove it from package.json. |
| 37 | + |
| 38 | +### When Updating Packages |
| 39 | + |
| 40 | +```bash |
| 41 | +# Use npm-check-updates or manually update |
| 42 | +npx ncu -u package-name |
| 43 | + |
| 44 | +# Or manually edit package.json with exact version |
| 45 | +``` |
| 46 | + |
| 47 | +Always: |
| 48 | +1. Update to exact version |
| 49 | +2. Run `npm install` |
| 50 | +3. Run `npm test` |
| 51 | +4. Verify no regressions |
| 52 | + |
| 53 | +### Checking for Version Ranges |
| 54 | + |
| 55 | +To find packages with version ranges: |
| 56 | + |
| 57 | +```bash |
| 58 | +# Find all dependencies with ^ or ~ |
| 59 | +grep -E '"\^|"~' package.json |
| 60 | +``` |
| 61 | + |
| 62 | +### Common Mistakes |
| 63 | + |
| 64 | +| Mistake | Fix | |
| 65 | +|---------|-----| |
| 66 | +| npm adds `^` by default | Remove `^` after install | |
| 67 | +| Copying from other projects | Verify versions are fixed | |
| 68 | +| Using `npm install package` without version | Specify exact version: `npm install [email protected]` | |
| 69 | + |
| 70 | +## devDependencies |
| 71 | + |
| 72 | +The same rules apply to devDependencies. All versions must be fixed. |
| 73 | + |
| 74 | +## peerDependencies |
| 75 | + |
| 76 | +peerDependencies may use ranges when necessary for compatibility with consuming projects, but this should be minimized. |
| 77 | + |
| 78 | +## Lock File |
| 79 | + |
| 80 | +The `package-lock.json` file must always be committed. It provides additional reproducibility even if someone accidentally introduces a version range. |
0 commit comments