Definition
npm audit sends a description of your dependency tree to the npm registry and reports the versions that have a known vulnerability in the GitHub Advisory Database. npm audit fix then installs patched versions where a compatible one exists.
The commands
| Command | What it does |
|---|---|
npm audit | Lists known vulnerabilities in the whole tree, direct and transitive |
npm audit --audit-level=high | Exits with an error only for high or critical issues; useful in CI |
npm audit --omit=dev | Ignores devDependencies |
npm audit --json | Machine-readable report |
npm audit fix | Installs patched versions that fit your existing version ranges |
npm audit fix --dry-run | Shows what would change, without changing it |
npm audit fix --package-lock-only | Updates the lockfile only, not node_modules |
npm audit fix --force | Also installs versions outside your ranges, including breaking major versions |
npm audit signatures | Verifies registry signatures and provenance attestations of installed packages |
Is npm audit fix --force safe?
It can break your build. --force lets npm install a new major version of a direct dependency, or sometimes an older one, if that removes the advisory. Your code can break. Run it on a branch, read the lockfile diff and run your tests before you merge.
When npm audit fix cannot fix it
- No patched version exists. Replace the package or accept the risk after you read the advisory.
- The patched version is outside the range a parent package allows. Wait for the parent to update, or force the version with
overrides:
{
"overrides": {
"semver": "7.5.4"
}
}
- The advisory does not apply to you. Many advisories are for code paths a build tool never runs. npm has no ignore list; teams record the decision and filter by
--audit-levelor--omit=dev.
What npm audit cannot see
npm audit is a lookup in a database. A version is flagged only after someone has reported it and an advisory is published. A hijacked release or a fresh typosquat has no advisory for its first hours or days, which is when it does its damage, so npm audit reports it as clean. Finding it requires reading the code: see npm audit vs source-code scanning. Togoder Security does that for each package in your lockfile; use it together with npm audit.
Frequently asked questions
What is the difference between npm audit and npm audit fix?
npm audit only reports known vulnerabilities. npm audit fix changes your lockfile and node_modules to install patched versions that are compatible with your version ranges.
Is it safe to run npm audit fix --force?
It can break your application, because it may install new major versions with breaking changes. Run it on a branch, review the lockfile diff and run your tests.
Does npm audit detect malware?
Only malware that is already reported: known malicious packages get advisories, and npm audit then flags them. A malicious version published today has no advisory yet and passes.
Why does npm audit show vulnerabilities that npm audit fix does not fix?
Either no patched version exists, or the patched version is outside the range that a parent package allows. Use overrides to force the version, or wait for the parent package to update.