Audit and Security in Dependencies
You'll learn how npm's built-in security tools help you discover and fix vulnerabilities in your project's dependencies.
The Security Risk in Dependencies
Every package you install brings code written by others into your project. Sometimes that code contains security vulnerabilities—bugs that could be exploited to harm your application or users. Since modern projects often depend on hundreds of packages (including transitive dependencies), manually tracking security issues is impossible.
npm audit: Your Security Scanner
The npm audit command scans your installed dependencies against a database of known vulnerabilities. It reads your package-lock.json to identify the exact versions you're using, then checks for any reported security issues.
npm audit
This command produces a report showing:
- Severity levels (low, moderate, high, critical)
- Which packages are vulnerable
- What the vulnerability allows attackers to do
- Recommended fixes (often just updating to a newer version)
Think of it like a health inspection for your project: it identifies which ingredients (dependencies) might be contaminated.
How Lockfiles Help Track Security
Your package-lock.json is crucial for security because it records the exact version of every package installed—not just ranges. This means:
- Reproducibility: You can prove which versions were installed when a vulnerability is discovered
- Audit accuracy:
npm auditchecks precise versions, not vague ranges - Controlled updates: You can review security patches before they're applied
When you run npm audit fix, npm updates vulnerable packages to safe versions and modifies your lockfile accordingly.
Key Takeaway: Use npm audit regularly to scan for vulnerabilities, and rely on your lockfile to track exactly which package versions are installed—this combination gives you both visibility and control over your project's security posture.