How to Fix npm Audit Vulnerabilities Without Breaking Changes
In this tutorial, you'll learn about How to Fix npm Audit Vulnerabilities Without Breaking Changes. We cover key concepts, practical examples, and best practices.
The Problem
You run npm audit and see 6 high severity vulnerabilities but npm audit fix wants to upgrade a package to a new major version that could break your app. Deep (transitive) dependencies are the usual suspects — you don't directly use them, but npm reports vulnerabilities in them and the fix requires upgrading your direct dependencies.
Quick Fix
1. Run a safe audit fix (patch/minor only)
npm audit fix
This applies fixes that won't break existing functionality (patch and minor version bumps only). Major version upgrades are intentionally skipped.
2. See what audit fix would change without applying
npm audit fix --dry-run --json
Review the output for proposed changes before applying.
3. Fix specific vulnerabilities manually
# Identify the vulnerable package
npm audit
# Output shows: "minimatch Regular Expression Denial of Service"
# Update just that package
npm install minimatch@latest --save-dev
4. Override deep (transitive) dependency versions
{
"overrides": {
"minimatch": "3.1.2",
"semver": "7.5.4",
"follow-redirects": "1.15.6"
}
}
This forces any sub-dependency to use the specified safe version, without changing your direct dependencies.
5. Use npm-force-resolutions (for older npm)
npx npm-force-resolutions
If npm < 8.2 doesn't support overrides, this tool patches package-lock.json.
6. Full audit fix including breaking changes
npm audit fix --force
Only use this if you're prepared to deal with breaking changes. Run your full test suite afterward.
7. Re-audit to verify fixes
npm audit
All previously reported vulnerabilities should now show 0 or a much lower count.
8. Set up automated dependency updates
// .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
Common Causes
| Audit Finding | Why It Happens | Safe Fix |
|---|---|---|
| High severity in deep dependency | A nested package is outdated | npm audit fix (safe) |
| Critical severity | Known CVE in a popular package | npm audit fix --force with caution |
| Multiple paths to same vulnerability | Two packages depend on different versions of the same vulnerable dep | Use overrides |
| No fix available | Package hasn't released a patch yet | Override the version manually |
Prevention
- Run
npm auditregularly (weekly CI check) - Use Dependabot or Renovate for automated dependency updates
- Review changelogs before accepting major version bumps
- Pin exact versions in
package.jsonfor production dependencies
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro