How do you handle security vulnerabilities in npm dependencies?
IntermediateConceptNode.js builds (npm, package.json)
Answer
Short answer
Scan regularly with npm audit (or Trivy, Snyk, Dependabot), fix by upgrading the vulnerable package (directly or via overrides for transitive ones), and fail the pipeline on high/critical issues that have a fix. Keep the lockfile committed so you know exactly what's installed.
Commands
npm audit # list known vulnerabilities
npm audit --audit-level=high # exit non-zero only for high/critical (good CI gate)
npm audit fix # apply compatible (non-breaking) upgrades
npm ls minimist # who depends on this package?
trivy fs --severity HIGH,CRITICAL . # scan the repo, including the lockfile
Fixing a transitive dependency
"overrides": {
"minimist": "1.2.8"
}
Process that works in teams
- Dependabot / Renovate opens PRs for updates automatically.
- CI runs
npm audit --audit-level=highor Trivy and fails on fixable criticals. - Accepted risks (no fix, not exploitable) are documented, not silently ignored.
- Images are re-scanned after build, since the base image has its own CVEs.
What to say in the interview
"We scan the lockfile in CI with npm audit or Trivy and fail on high/critical with available fixes. Dependabot keeps packages current, overrides pin safe versions of transitive packages, and we rescan the final image too."
More on Node.js builds (npm, package.json)
What is package.json and what does it contain?BeginnerHow do you build and run a Node.js application?BeginnerWhat is the difference between npm install and npm ci?BeginnerWhy must package-lock.json be committed to Git?BeginnerWhat is the difference between dependencies and devDependencies?BeginnerWhat do the ^ and ~ symbols mean in package.json versions?Beginner