Course contentsShow
← Security
Lesson 1295 of 3,05226. Application Security - Supply ChainFree lesson

npm Package Verification

Using npm audit, package-lock.json integrity fields, and verifying npm package publishers and signatures.

npm Package Verification

What you'll learn: How to verify npm packages are trustworthy using built-in audit tools, integrity checks, and publisher verification.

The Problem: Can You Trust That Package?

You run npm install and download code from strangers on the internet directly into your project. How do you know it hasn't been tampered with? How do you verify the author is who they claim to be? npm provides several verification mechanisms to help answer these questions.

npm audit: Your First Line of Defense

The npm audit command scans your installed packages against a database of known vulnerabilities:

npm audit

This checks your dependency tree and reports any security issues with severity ratings. Think of it as a health check for your dependencies. Run npm audit fix to automatically update vulnerable packages where safe to do so.

package-lock.json Integrity Fields

Every entry in package-lock.json includes an integrity field containing a cryptographic hash (like a fingerprint):

"lodash": {
  "version": "4.17.21",
  "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
  "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
}

When you install, npm verifies the downloaded package matches this hash. If someone tampers with the package on the registry, the hash won't match and installation fails—like a tamper-evident seal.

Verifying Publishers

Check package publisher information before installing:

npm view <package-name>

Look for verified publishers, download counts, and recent activity. Be wary of packages with few downloads or newly created accounts mimicking popular packages.

Key Takeaway: Use npm audit regularly, rely on package-lock.json integrity checks for tamper detection, and verify package publishers manually before adding new dependencies to your project.