Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Lockfiles and Security

npm audit and Vulnerabilities

Learn how npm audit, yarn npm audit and pnpm audit find vulnerable dependencies, how to read severity and paths, when audit fix is safe, and how to triage noise.

Your project probably has hundreds of packages you never chose directly. When a security researcher reports a flaw in one of them, the advisory is added to a public database, and your package manager can check your lockfile against it. That check is audit. It is a valuable alarm, but also a noisy one, and this lesson teaches you to read it, fix what is real and ignore what is not.

Audit commands appear as read-only snippets (the registry's advisory service is not reachable from this site). The runnable examples model the matching logic with plain Node.js.

Running an audit

Terminal
npm audit                       # report against the lockfile
npm audit --omit=dev            # only what ships to production
npm audit --audit-level=high    # exit non-zero only for high or critical
npm audit --json                # for scripts and CI
yarn npm audit                  # Yarn Berry (Classic: yarn audit)
pnpm audit
pnpm audit --prod

The tool sends your lockfile's package names and versions (not your code) to the registry's audit endpoint and prints the advisories that match. A typical entry has:

  • the package and affected range (semver <5.7.2),
  • a severity: low, moderate, high or critical (derived from CVSS, a 0 to 10 score),
  • the dependency path that pulled it in (app > webpack > ... > semver),
  • an advisory URL with details and the patched version.

How matching works

Advisories are data: a package name, a vulnerable range and a patched range. Audit walks every locked package and tests its version against the vulnerable range. This stand-in does exactly that, using a small subset of range syntax.

Node.js Stand-in: match locked versions against advisories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that the second semver (7.6.0) is not flagged. Different copies of a package can sit at different versions; only the vulnerable copy matters, and the path tells you who to blame.

Fixing vulnerabilities

Work from least to most invasive:

  1. npm audit fix updates vulnerable packages to patched versions within the ranges your dependents allow. It is usually safe, and it changes only the lockfile. Yarn Berry has yarn up -R name (update recursively); pnpm has pnpm update name -r and pnpm audit --fix, which writes overrides.
  2. Update the direct dependency that pulls the vulnerable one in. Often the parent already released a version that depends on the patched one.
  3. Override the transitive version when the parent is slow to update (see overrides and resolutions).
  4. npm audit fix --force allows semver-major upgrades. Treat it as a suggestion to read, not a button to press: it can break your build or swap a package for a very different one.
  5. Replace the package if it is abandoned.

Re-run the audit after every step, and run your tests.

Triage: not every finding is an emergency

A CVSS score describes the flaw in general, not in your application. Ask:

  • Is it reachable? A regex denial-of-service in a build-time CLI used only by your CI is much less urgent than one in a request handler.
  • Is it in production or only in devDependencies? Use --omit=dev / --prod to separate the two.
  • Is a fix available? If not, can you avoid the vulnerable function, add input limits, or place a proxy in front?
  • Is the advisory real for your usage? Some advisories depend on a specific option or Node version.

Record your decisions instead of silencing the tool. For example, pnpm supports pnpm.auditConfig.ignoreCves and npm teams often keep an "accepted risks" file with a reason and a review date. Silencing an alert forever without a note is how forgotten risks happen.

This stand-in prioritizes findings with a simple scoring rule, to show how triage thinking can be written down.

Node.js Stand-in: prioritize findings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Audit in CI

  • Fail the build on --audit-level=high for production dependencies, and report the rest as warnings.
  • Run the audit on a schedule too, not only on pull requests: a new advisory can appear against code that has not changed.
  • Enable automatic update pull requests (Dependabot or Renovate) so patches arrive as small, reviewable changes.
  • Remember what audit cannot see: malicious packages that have no advisory yet, flawed code in your own dependencies' scripts, and compromised maintainer accounts. That is the topic of supply-chain security.

An audit only reads your lockfile. With no lockfile, npm audit has to build a tree first and may check a different tree than the one your teammates run. Another reason to commit it (see lockfiles explained).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What information does npm audit send to the registry?

    Audit needs only the dependency tree (names and versions) to look up matching advisories.
  2. 2. Which command is the least invasive way to fix a vulnerability?

    npm audit fix only moves versions inside existing ranges. --force can install breaking major versions.
  3. 3. A high-severity flaw is in a devDependency used only by a local build script. What is the sensible reading?

    Reachability and environment matter. Dev-only tools usually run in trusted contexts, yet fixes are cheap and keep alerts clean.
  4. 4. Why run the audit on a schedule as well as on pull requests?

    A vulnerability report can appear long after you installed the package, so periodic checks catch new advisories.

Summary

  • Audit matches your locked versions against a public advisory database and prints severity and dependency paths.
  • Fix in order: audit fix, update the parent, override, replace; avoid blind --force.
  • Triage by reachability, production exposure and fix availability, and record accepted risks.
  • Run audits in CI on pull requests and on a schedule, plus automated update PRs.
  • Audit cannot see every threat; malicious packages need other defenses.

Next: Supply-Chain Security