npm audit is not enough: how to keep your npm dependencies under control
From the work/ 12 min read
What npm audit sees and what it misses, plus the rest: the lockfile, vetting packages with npm view, updating in small steps and npm 12 install script approval.
- Node.js
- Security
- JavaScript
In this article (8)
npm audit only compares the versions in your dependency tree against security advisories that already exist. It does not see a malicious package that has no advisory yet, an install script that already ran, or an abandoned library. To cover that: commit the lockfile, review each dependency before adding it, update in small steps and control install scripts.
We ran everything below on November 19, 2026 in a test project, with npm 12.2.3 and npm 11 on Node 22. Since npm 12.0.0, released on July 8, 2026, dependency install scripts no longer run until you approve them; how that works is further down.
What npm audit sees and what it misses
According to the npm audit documentation, the command sends a description of your project's dependencies (names and versions) to your registry and asks for a report of known vulnerabilities. The links in the report point to the GitHub Advisory Database. It is a lookup against a list: if the installed version falls in an advisory's range, it shows up; if there is no advisory, it does not. This is what it looks like on a project with a single direct dependency, express@4.17.1, an old version picked on purpose (output trimmed):
$ npm audit
# npm audit report
qs <=6.16.0
Severity: high
qs vulnerable to Prototype Pollution - https://github.com/advisories/GHSA-hrpp-h998-j3pp
...
fix available via `npm audit fix`
node_modules/qs
7 vulnerabilities (3 low, 4 high)
To address all issues, run:
npm audit fix
qs <=6.16.0is the package and the affected range. You never installedqs: it comes in through express and body-parser.Severity: highand the link to theGHSA-…advisory, which describes the flaw and the fixed version.fix available via npm audit fixmeans a fixed version exists inside the ranges in yourpackage.json.- The summary counts seven vulnerable packages: body-parser, cookie, path-to-regexp, qs, send, serve-static and express itself, which depends on them.
That is useful and worth running. The problem is what it does not see:
- Anything without an advisory yet. If someone publishes a malicious version of a package you use, npm audit says nothing until the advisory exists. That window is exactly when it gets installed.
- The order. The audit runs at the end of
npm install, after the dependencies' install scripts have already run (on npm 11 and earlier, which run them without asking). - Maintenance. A package untouched for years, published by a single account, or deprecated, is not a known vulnerability.
- Tree size. Audit does not tell you that one line in your
package.jsonpulled in 50 packages. - Anything outside the lockfile: what you fetch with
npx, global tools, scripts from a CDN or code copied by hand.
And the other way round: it reports a vulnerability even if your code never touches the affected part. --audit-level=high only changes the severity at which the command exits with an error; the docs state that it does not filter the report.
Scroll the figure sideways to see all of it
| Risk | Does npm audit see it? | What covers it |
|---|---|---|
| Version with a published advisory | Yes | npm audit, updating |
| Malicious version with no advisory yet | No | Lockfile, npm ci, waiting a few days before adopting new versions |
| Install script that runs code | No | allowScripts in npm 12, checking hasInstallScript |
| Abandoned or deprecated package | No | npm view before adding it |
| Huge tree from a single dependency | No | npm ls --all, a trial install |
| Vulnerability in code you do not use | Yes, reported anyway | Read the advisory and decide |
Pin versions with the lockfile
When you install express@4.17.1, npm saves the range ^4.17.1 in package.json, which accepts any later 4.x. What pins the exact version is package-lock.json: per its documentation, it describes the exact tree that was generated so later installs produce the same one, and it is meant to be committed. This is the express entry in the test project's lockfile:
"node_modules/express": {
"version": "4.17.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
"integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
...
}
- The key is the path inside
node_modules: the lockfile stores the whole tree, not just your direct dependencies. versionis the exact version installed, even though the range allows others.resolvedsays where the package was downloaded from.integrityis the hash of the downloaded file. If the content changes, the install fails instead of installing something else.
With the lockfile committed, npm install kept 4.17.1 even though 4.22.3 already existed inside the range. For servers and CI, use npm ci: per its documentation, it requires a lockfile, exits with an error when it does not match package.json instead of changing it, deletes node_modules before installing and never writes to either file. What gets deployed is exactly what you tested. If you build an image, the same npm ci goes in the dependencies stage of a multi-stage Dockerfile.
Scroll the figure sideways to see all of it
Review a dependency before you add it
The best dependency is the one you do not add. If you need it, check three things before npm install: who maintains it, when the last version came out and how many packages it brings. npm view reads the public registry without installing anything:
$ npm view express dist-tags.latest time.modified repository.url
dist-tags.latest = '5.3.0'
time.modified = '2026-11-07T17:35:30.254Z'
repository.url = 'git+https://github.com/expressjs/express.git'
$ npm view express maintainers
$ npm view request deprecated
request has been deprecated, see https://github.com/request/request/issues/3142
$ npm view esbuild scripts
{ postinstall: 'node install.js' }
dist-tags.latestis the version thatnpm install expressinstalls when you give no number.time.modifiedis the last time the package changed on the registry. Years without changes is not bad on its own, but together with open advisories it is a warning sign.repository.urltakes you to the code. With no repository, you cannot read what you install.maintainerslists the accounts that can publish. A single account is a single point of failure.deprecatedreturns the deprecation message, or nothing if the package is not deprecated.scriptsshows whether the current version has apostinstallor other scripts that run on install.
To see how many packages it brings, do a trial install in an empty folder with npm install express --package-lock-only (it only writes the lockfile) and count with npm ls --all --parseable. We measured it: express@5.3.0 brings 65 packages in total and express@4.17.1, 50. npm ls without --all shows only your direct dependencies, and npm explain qs answers why a package you never asked for is installed:
$ npm explain qs
qs@6.7.0
node_modules/qs
qs@"6.7.0" from body-parser@1.19.0
node_modules/body-parser
body-parser@"1.19.0" from express@4.17.1
node_modules/express
express@"^4.17.1" from the root project
That is how, in the post on the Next.js 14 RCE, we found which sharp version Next was loading: the vulnerability sat in a dependency nobody had added by hand.
Scroll the figure sideways to see all of it
Update a little at a time
npm outdated compares what is installed with what your range allows and with the latest version. It exits with code 1 when anything is behind:
$ npm outdated
Package Current Wanted Latest Location Depended by
express 4.17.1 4.22.3 5.3.0 node_modules/express demo-store
$ npm update express
added 18 packages, changed 23 packages, and audited 69 packages in 2s
found 0 vulnerabilities
Currentis what is installed;Wanted, the newest your^4.17.1range accepts;Latest, the newest published, which here is a major version.npm update expressmoved to 4.22.3, inside the range, and the audit dropped to zero. It also added 18 packages: an update changes the tree, which is why you review it.- Moving to 5.3.0 is a major change: it goes on its own, after reading the release notes and with your tests running.
One dependency per commit, tests between each one and the lockfile in the same commit. If something breaks, you know what it was and you revert it without dragging the rest along. Do not reach for npm audit fix --force by reflex: the docs warn that it can install versions outside your range, major changes included.
To avoid being among the first to install a freshly published version, npm has min-release-age (in days; config documentation). With --min-release-age=60, npm install express picked 5.2.1 instead of 5.3.0, published on October 9, 2026; we tested it on npm 12.2.3 and 11.11.1. An urgent security patch is the exception: you update right away, as we covered with the November Next.js security release. And do not forget the runtime: npm 12 requires Node ^22.22.2 || ^24.15.0 || >=26.0.0, and updating Node in Docker walks through the process.
Read the install scripts (npm 12 blocks them)
A package can declare preinstall, install or postinstall, and those commands run on your machine or server when it is installed, with your permissions. It is the classic route for malicious packages, and it runs before npm audit has anything to say.
The npm 12.0.0 release notes change that: dependency scripts are blocked by default unless the allowScripts field in the root package.json allows them. Per the npm install-scripts documentation, the block covers preinstall, install, postinstall and, for dependencies that do not come from the registry, prepare. Your own project's scripts still run. This is what we saw with npm 12.2.3:
$ npm install esbuild@0.25.0
added 2 packages, and audited 3 packages in 2s
found 0 vulnerabilities
npm warn install-scripts 1 package had install scripts blocked because they are not covered by allowScripts:
npm warn install-scripts esbuild@0.25.0 (postinstall: node install.js)
$ npm install-scripts approve esbuild
Approved esbuild:
added esbuild@0.25.0
$ npm rebuild
rebuilt dependencies successfully
- The install succeeds and npm audit says
found 0 vulnerabilities, but esbuild'spostinstalldid not run. - The warning names the package and the command that were blocked.
npm install-scripts lslists what is pending. approvewrites"allowScripts": { "esbuild@0.25.0": true }topackage.json, pinned to that version.npm rebuildruns the newly approved scripts.
Because the approval is pinned to the version, when we moved to esbuild 0.25.1 the script was blocked again until we approved it again: every new version goes through your review. npm install-scripts deny esbuild stores "esbuild": false for every version and stops the warning. Many packages work without their script; in our test, esbuild 0.28.2 kept transforming TypeScript with its script denied. Not all of them: a native module that compiles on install needs it. Read the script (npm view <package> scripts) and decide.
In CI, put strict-allow-scripts=true in the project's .npmrc: a dependency with unreviewed scripts then makes npm ci fail with ESTRICTALLOWSCRIPTS instead of just warning. Passing --allow-scripts in a project install is an error; that flag is for global installs and npx.
If you are still on npm 11, the scripts run. npm 11.21.3 warns ("not yet covered by allowScripts") but runs them, and it already has npm install-scripts approve, so you can have the list ready before moving to npm 12; npm 11.11.1 ran them with no warning. To see which packages in your lockfile have scripts on any version, this script reads the hasInstallScript field:
// list-install-scripts.mjs: dependencies in package-lock.json that run code when installed
import { readFileSync } from "node:fs";
const lock = JSON.parse(readFileSync("package-lock.json", "utf8"));
let count = 0;
for (const [path, pkg] of Object.entries(lock.packages ?? {})) {
if (path === "" || !pkg.hasInstallScript) continue;
const i = path.lastIndexOf("node_modules/");
const name = i === -1 ? path : path.slice(i + "node_modules/".length);
console.log(name + "@" + pkg.version + " " + (pkg.resolved ?? "(no resolved URL)"));
count++;
}
console.log(count + " package(s) with install scripts");
- It reads
package-lock.jsonfrom the current folder. It needs a version 2 or 3 lockfile (npm 7 onward), which has thepackagesobject. - It walks every entry; the empty key
""is your own project and is skipped. hasInstallScriptis the field npm sets when the package has apreinstall,installorpostinstall.- The name comes from the last
node_modules/in the path, so scoped (@scope/name) and nested packages work; a workspace prints with its path. - It prints name, version and origin, then the total.
On the test project it printed esbuild@0.28.2 https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz and 1 package(s) with install scripts. Run it before and after every update: if a new package shows up on the list, read it before you approve it.
Common mistakes
- Treating "found 0 vulnerabilities" as a clean bill of health. It only means there are no published advisories for those versions.
- Not committing
package-lock.json. Each install can resolve different versions and the server does not run what you tested. - Using
npm installin CI. It can rewrite the lockfile;npm cifails when it does not match. - Running
npm audit fix --forcewithout reading. It can bring major changes into your tree. - Updating everything at once. If something breaks, you do not know what it was.
- Approving every script with
--allto silence the warning. You are back to npm 11 behavior. - Reviewing only your direct dependencies. Most advisories in the example were in packages nobody added by hand.
Frequently asked questions
Does npm audit catch malicious packages?
Only once an advisory exists. The GitHub Advisory Database includes malware advisories, but until someone reports it, the malicious version passes the audit. That is why the lockfile, waiting before adopting new versions and blocking scripts matter.
Do I need npm 12 to block install scripts?
It is the most direct route, because npm 12 blocks them by default. On npm 11 they run; you can use --ignore-scripts, which turns off all of them, your project's included, or prepare allowScripts with npm install-scripts approve on the npm 11 releases that already ship it.
Why does npm install-scripts deny give me ENOMATCH?
The command works on what is installed: if the package is not in node_modules, it has nothing to apply to. Install first without strict mode, then approve or deny, and only then turn on strict-allow-scripts.
How often should I update dependencies?
Check npm outdated and npm audit on a fixed schedule, for example weekly, and apply security patches as soon as they ship. Major versions go one at a time, with time to test them.
Checklist
package-lock.jsonis committed and CI installs withnpm ci.npm audit --audit-level=highruns in CI and you read the advisories it reports.- Before adding a dependency you check
npm view: version, date, repository, publishing accounts, deprecation and scripts. - You know how many packages it brings with
npm ls --all. - You update one dependency per commit, with tests, and majors separately.
- You use
min-release-ageor wait a few days before adopting new versions, except for security patches. - On npm 12,
allowScriptsonly lists packages you reviewed andstrict-allow-scripts=trueis in.npmrc. - On npm 11, you know which packages have install scripts and you review them on every update.
None of these replaces the others: npm audit covers what is known, the lockfile and npm ci cover what gets installed, and your review covers what enters the project.
Saw this on Instagram? Comment DEPENDENCIAS on the Reel and I'll send you the guide.
Written by

Published November 19, 2026