How to Fix npm audit High Vulnerabilities
In this tutorial, you'll learn about How to Fix npm audit High Vulnerabilities. We cover key concepts, practical examples, and best practices.
The Problem
npm audit reports high or critical vulnerabilities in your dependencies, indicating known security issues in packages that could be exploited in production.
Quick Fix
Step 1: Run npm audit fix
Auto-fix compatible vulnerabilities:
npm audit fix
For breaking changes, use --force (careful with major version changes):
npm audit fix --force
Step 2: Check the audit report
Understand which packages are affected:
npm audit
=== npm audit security report ===
High Prototype Pollution
Package lodash
Patched in >=4.17.21
Dependency of express-handlebars
Path express-handlebars > lodash
Step 3: Update the direct dependency
If a sub-dependency is vulnerable, update the parent:
npm install express-handlebars@latest
Step 4: Use overrides to force a sub-dependency version
When the direct dependency has not updated its vulnerable sub-dependency:
{
"overrides": {
"lodash": "4.17.21"
}
}
Then run:
npm install
Step 5: Replace deprecated packages
Some packages are no longer maintained:
# Replace request (deprecated) with node-fetch or axios
npm uninstall request
npm install node-fetch
Update your imports accordingly.
Step 6: Audit only production dependencies
Dev dependencies are not exposed in production:
npm audit --production
This focuses on vulnerabilities that actually affect production.
Prevention
- Run
npm auditin CI to fail builds on high vulnerabilities. - Use
npm outdatedto track outdated packages. - Enable Dependabot or Renovate for automated dependency updates.
- Pin exact versions for critical dependencies.
- Regularly review and remove unused dependencies.
Common Mistakes with npm audit
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world NODE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro