tezvyn:

Dependency Scanning with npm audit

AI-drafted, machine-checkedSource: docs.npmjs.combeginner
Dependency Scanning with npm audit

Think of dependency scanning as a background check for your code. `npm audit` compares your project's packages against a database of known security flaws, telling you if you're using vulnerable code. The biggest footgun is blindly running `npm audit fix`.

WHY IT EXISTS Modern applications depend on hundreds or thousands of third-party packages. A single vulnerability in a transitive dependency can compromise your entire system. Manually tracking security advisories for every package is impossible, so automated tools are needed to manage this risk.

THE MENTAL MODEL Think of npm audit as an automated security background check for your project's dependencies. It takes your complete list of packages and their exact versions, then compares it against a public database of known vulnerabilities. It doesn't find new bugs; it only flags code that is already known to be insecure.

HOW IT WORKS When you run npm audit, the npm CLI creates a summary of your project's dependencies from the package-lock.json or npm-shrinkwrap.json file. It sends this data to your configured npm registry. The registry checks this dependency tree against the npm Security Advisories database. If any vulnerabilities are found, the registry returns a report detailing the vulnerability, its severity (low, moderate, high, critical), the affected package, and the dependency path that introduced it.

WHEN TO USE IT Use npm audit frequently during local development to catch vulnerabilities as soon as a package is added. Most importantly, integrate it into your Continuous Integration (CI) pipeline to automatically fail any build that introduces a new vulnerability above a certain severity threshold, preventing insecure code from ever being deployed.

WHEN NOT TO USE IT Do not rely on npm audit as your only security measure. It only finds known vulnerabilities in public npm packages. It will not find security flaws in your own application code, nor will it detect zero-day exploits. Also, be cautious with the npm audit fix command. While convenient, it can automatically perform major version upgrades that introduce breaking changes. Always review the proposed changes before applying them.

ONE CANONICAL EXAMPLE A developer's project uses an old version of the express package. A new vulnerability is discovered in one of express's own dependencies, like the qs parsing library. Running npm audit will now produce a report flagging a high-severity vulnerability. It will show that qs is the problem and that it was included via express. The report will suggest a remediation, such as running npm audit fix or updating express to a newer, non-vulnerable version.

Read the original → docs.npmjs.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.