Guides / Languages and packages

npm audit, lockfiles and npm ci

beginnerUpdated 26 Nov 20261184 words

A Node.js app installs its own dependencies and every package they depend on. The lockfile records each of them with its exact version, and npm audit checks that list against the advisories of the npm registry. Both are part of npm, so the setup below does not need extra tools.

Commit the lockfile and install with npm ci

package.json holds version ranges such as ^4.17.20. package-lock.json holds the versions that were resolved, for every package in the tree. Commit it. Without it, two installs a week apart can produce different trees, and an audit on your laptop says nothing about the server.

On servers, in CI and in Dockerfiles, install with npm ci:

npm ci --omit=dev

npm ci differs from npm install in ways that matter for deployments:

  • It fails when there is no package-lock.json.
  • It fails when the lockfile and package.json do not match, rather than updating the lockfile.
  • It deletes node_modules first and never writes to package.json or the lockfile.

In a Dockerfile, copy the two files before the source so that the install layer is cached:

COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .

Run npm audit

npm audit reads the lockfile, sends the names and versions of the packages to the registry and prints the advisories that match:

npm audit

In a project with lodash locked at 4.17.20 and minimist 1.2.5 as a development dependency (npm 11.19.0, November 2026):

# npm audit report

lodash  <=4.17.23
Severity: high
Command Injection in lodash - https://github.com/advisories/GHSA-35jh-r3h4-6jhm
Regular Expression Denial of Service (ReDoS) in lodash - https://github.com/advisories/GHSA-29mw-wpgm-hmr9
lodash vulnerable to Code Injection via `_.template` imports key names - https://github.com/advisories/GHSA-r5fr-rjxr-66jc
Lodash has Prototype Pollution Vulnerability in `_.unset`, `_.omit`, and `_.pullAt` functions - https://github.com/advisories/GHSA-xxjr-mmjv-4gpg
fix available via `npm audit fix`
node_modules/lodash

minimist  1.0.0 - 1.2.5
Severity: critical
Prototype Pollution in minimist - https://github.com/advisories/GHSA-xvch-5gv4-984h
fix available via `npm audit fix`
node_modules/minimist

2 vulnerabilities (1 high, 1 critical)

The report groups advisories by package, with the affected version range and the highest severity. Each line links to a GitHub advisory (GHSA-…) with the CVE id and the details. fix available via npm audit fix means a fixed version fits the ranges in package.json.

Development tools such as test runners and bundlers do not run on the server. To audit only what ships:

npm audit --omit=dev
lodash  <=4.17.23
Severity: high
...
1 high severity vulnerability

Fail CI on serious findings

npm audit exits with a non-zero code when it finds any advisory. --audit-level raises that threshold; it does not hide lower findings from the output:

npm audit --omit=dev --audit-level=high

In the example, this exits with 1 (lodash is high). --audit-level=critical with --omit=dev exits with 0, and without --omit=dev with 1 again, because of minimist.

A GitHub Actions job:

name: npm audit
on:
  push:
  pull_request:
  schedule:
    - cron: "17 6 * * *"
jobs:
  audit:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 24
      - run: npm ci --ignore-scripts
      - run: npm audit --omit=dev --audit-level=high

The scheduled run finds advisories that are published on days without commits.

Fix findings

Inside your version ranges

npm audit fix

This updates the lockfile (and node_modules) to fixed versions that still match the ranges in package.json. In the example, lodash moves from 4.17.20 to 4.18.1 within ^4.17.20, and the next npm audit prints found 0 vulnerabilities. Review the lockfile diff and run your tests. npm audit fix --package-lock-only changes only the lockfile, without touching node_modules.

Outside your version ranges

When the fix is outside the range in package.json, the report says so:

fix available via `npm audit fix --force`
Will install lodash@4.18.1, which is outside the stated dependency range

npm audit fix --force installs such versions, including semver-major updates, for every package in the report at once. Update one package at a time instead and read its changelog:

npm install lodash@^4.18.1

A vulnerable package deep in the tree

Sometimes the vulnerable package is a dependency of a dependency, and the package in the middle pins the old version. mkdirp 0.5.1 requires minimist at version 0.0.8:

minimist  <=0.2.3
Severity: critical
Prototype Pollution in minimist - https://github.com/advisories/GHSA-vh95-rmgr-6w4m
Prototype Pollution in minimist - https://github.com/advisories/GHSA-xvch-5gv4-984h
fix available via `npm audit fix --force`
Will install mkdirp@0.5.6, which is outside the stated dependency range
node_modules/minimist
  mkdirp  0.4.1 - 0.5.1
  Depends on vulnerable versions of minimist

The best fix is a newer mkdirp. When the package in the middle does not have a release with the fix, overrides in package.json forces a version for the whole tree:

{
  "dependencies": {
    "mkdirp": "0.5.1"
  },
  "overrides": {
    "minimist": "1.2.8"
  }
}

Run npm install after the change and check which version is installed:

npm ls minimist
audit-demo2@1.0.0 /src
`-- mkdirp@0.5.1
  `-- minimist@1.2.8 overridden

An override can break the package in the middle if the forced version changed its API; run your tests.

Remove the override once the parent package depends on a fixed version. A forgotten override keeps the package at that version when later fixes come out.

Install scripts

Packages can run preinstall, install and postinstall scripts during npm ci. A malicious or compromised package can use them to run code on the machine that installs it. --ignore-scripts turns them off:

npm ci --ignore-scripts

Some packages need their install script, for example those that compile native code. Try the flag in CI first; if the app still builds and its tests pass, keep it.

npm audit signatures checks the registry signatures of the installed packages and their provenance attestations where they exist:

npm audit signatures
audited 4 packages in 2s

4 packages have verified registry signatures

pnpm

pnpm has pnpm audit, with --prod for the production dependencies. Its --audit-level behaves differently from npm and filters the printed advisories as well as the exit code. With pnpm 12.23.0 and a project with high advisories, pnpm audit --prod --audit-level high printed only the high ones and exited with 1. --audit-level critical printed no advisory and exited with 0; the summary line still counted all of them. pnpm audit --fix adds overrides to pnpm-workspace.yaml.

On the server

A Node.js app on a server has three layers of packages: the Node.js runtime itself (from the distribution, NodeSource or the container base image), the operating system packages, and the npm tree in node_modules. npm audit covers only the npm tree. Keep the runtime on a supported release line and scan the image or the host for the rest (/guides/scan-images-in-ci).

The Patchbrief agent can read the package-lock.json of the app paths you configure and checks it against new advisories every day.

Sources

  • npm audit — what is sent to the registry, exit codes, --audit-level, --omit, fix --force, --package-lock-only, audit signatures
  • npm ci — differences from npm install, --ignore-scripts
  • package.json: overrides
  • pnpm audit — --audit-level, --fix, exit code
  • GHSA-35jh-r3h4-6jhm — lodash command injection (CVE-2021-23337), used in the example

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free