OCImendby CloudTrace

How to fix CVEs in Node.js container images

Short answer

A Node.js image carries CVEs in the base OS, in Node.js itself, in the npm that ships with it, and in node_modules. Rebuild on the newest patch tag of your Node.js line, upgrade OS packages, run npm audit fix (with overrides for transitive dependencies), install production dependencies only, and drop npm from the runtime image if the app does not need it.

Fix a Node.js image now → free, no sign-up; pre-filled with node:22-slim, change it to yours

The steps

  1. 01Newest patch of your Node.js lineNode.js security releases ship as patch versions of each line.
  2. 02Production dependencies onlynpm ci --omit=dev: test and build tools stay out of the image.
  3. 03Fix transitive dependenciesnpm audit fix, or overrides in package.json when a parent pins an old version.
  4. 04Upgrade OS packagesIn the final stage, after copying the app.

By hand

FROM node:22-slim
RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev && npm audit fix --omit=dev || true
COPY . .

Then rescan: the CVEs that remain are either without a released fix or in code the application never loads (the report marks both).

Traps that bring CVEs back

Node.js images in OCImend's reports

Questions

How do I fix CVEs in a Node.js container image?
A Node.js image carries CVEs in the base OS, in Node.js itself, in the npm that ships with it, and in node_modules. Rebuild on the newest patch tag of your Node.js line, upgrade OS packages, run npm audit fix (with overrides for transitive dependencies), install production dependencies only, and drop npm from the runtime image if the app does not need it.
Does npm audit fix the image?
It fixes node_modules only; OS packages and Node.js itself need a newer base or package upgrades.
Alpine or slim for Node.js?
Both are maintained; slim avoids musl-specific native module issues.

Other image types

Python · Java · Go · nginx · PostgreSQL · Alpine · Debian · Ubuntu · Red Hat UBI

Fix a Node.js image now →