← Workflows

阅读中文版本

NPM Audit Fix Workflow

Run npm audit to identify vulnerabilities and apply fixes in a controlled manner with testing after each fix.

beginner~20 minnpmsecurityworkflow

Objective

Resolve known vulnerabilities in project dependencies without breaking the build.

Environment

  • A Node.js project with package-lock.json
  • NPM version 7 or later
  • CI pipeline that runs tests on push

Steps

1. Run npm audit

Execute npm audit to get a report of all vulnerabilities.

Expected state: A vulnerability report is generated with severity levels.

2. Apply audit fix

Run npm audit fix to apply non-breaking fixes automatically.

Expected state: Non-breaking vulnerabilities are patched in package-lock.json.

3. Run tests

Execute the full test suite to verify that fixes did not break anything.

Expected state: All tests pass with no regressions.

4. Review remaining

Run npm audit again to check for remaining vulnerabilities that need manual attention.

Expected state: A reduced list of vulnerabilities requiring manual fixes is shown.

Decision points

Should you use --force?

  • Yes, for breaking fixes
  • No, only non-breaking fixes

Start without --force; use it only if you have reviewed the breaking changes and have a rollback plan.

Expected outcome

All automatically fixable vulnerabilities are resolved, tests pass, and remaining vulnerabilities are documented for manual review.

Verification

Run npm audit and confirm zero high or critical vulnerabilities; run the test suite and confirm all tests pass.

Cleanup

Commit the updated package-lock.json and package.json if changed.

Audit first, fix second

Always review the audit report before applying fixes. Some fixes may introduce breaking changes.

npm audit
npm audit fix
npm test

Implementation Notes

  1. Run npm audit — Execute npm audit to get a report of all vulnerabilities. Expected state: A vulnerability report is generated with severity levels.

  2. Apply audit fix — Run npm audit fix to apply non-breaking fixes automatically. Expected state: Non-breaking vulnerabilities are patched in package-lock.json.

  3. Run tests — Execute the full test suite to verify that fixes did not break anything. Expected state: All tests pass with no regressions.

  4. Review remaining — Run npm audit again to check for remaining vulnerabilities that need manual attention. Expected state: A reduced list of vulnerabilities requiring manual fixes is shown.

Common Pitfalls

  • Should you use –force? Options: Yes, for breaking fixes / No, only non-breaking fixes. Recommendation: Start without –force; use it only if you have reviewed the breaking changes and have a rollback plan.