Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

npm audit fix broke the build and you want your old dependencies back

Errornpm audit fix --force installed major versions and the app no longer builds

Short answer

npm has no undo command for audit fix. It edits package-lock.json, and with --force also package.json. Restore those files from git, then run npm ci to rebuild node_modules from the restored lockfile. If the changes were already committed, restore from the commit before them.

  • Applies to npm 10, Node.js 22, git 2.4x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See exactly what the audit fix changed. Only files git lists here can be restored.

    git status --short
    git diff --stat package.json package-lock.json
  2. 2Match what you see, then jump to its fix

    1. 1git status shows package-lock.json modified, package.json unchangedThe fix only changed the lockfileFix
    2. 2git status shows package.json modified as well, with higher version ranges--force moved dependencies to new major versionsFix
    3. 3git status is clean, but git log shows the dependency update commitThe audit fix was already committedFix

What does the error look like?

  • npm audit fix --force installed major versions and the app no longer builds
  • git status shows package.json and package-lock.json modified after npm audit fix

Cause 1 of 3

The fix only changed the lockfile

1Check

Without --force, audit fix updates vulnerable packages to versions that still fit the ranges in package.json. The tree and the lockfile change; package.json stays.

git diff --stat package.json package-lock.json

2Fix

Restore the lockfile from the last commit, then reinstall from it. npm ci removes node_modules first and never writes to package.json or the lockfile.

git restore --source=HEAD package-lock.json
npm ci

3Verify

git reports no change to the lockfile and the build passes again.

git status --short
npm run build

Confirmed by docs.npmjs.com

Did this fix it?

Cause 2 of 3

--force moved dependencies to new major versions

1Check

--force lets audit fix install modules outside your stated ranges, including semver-major changes, so package.json can change too.

git diff package.json

2Fix

Restore both files from the last commit and reinstall. Use the dry run next time to preview before changing anything.

git restore --source=HEAD package.json package-lock.json
npm ci
npm audit fix --dry-run

3Verify

The versions in package.json match the last commit and npm ls shows the old tree.

git diff --stat package.json package-lock.json
npm ls --depth=0

Confirmed by docs.npmjs.com

Did this fix it?

Cause 3 of 3

The audit fix was already committed

1Check

A clean working tree means there is nothing to discard. The old files still exist in the earlier commit, and git restore can take them from any commit.

git log --oneline -n 5 -- package-lock.json

2Fix

Restore both files from the commit before the audit fix, reinstall from them, and commit the result.

git restore --source=HEAD~1 package.json package-lock.json
npm ci

3Verify

The diff against the earlier commit is empty for both files.

git diff HEAD~1 -- package.json package-lock.json

Confirmed by git-scm.com

Did this fix it?

How do I stop it coming back?

  • Commit package.json and package-lock.json before running npm audit fix, so a restore is one command.
  • Run npm audit fix --dry-run first to see what would change without changing anything.
  • Avoid --force on a project you cannot test straight away; it is the option that crosses major versions.
  • Keep package-lock.json in source control; npm documents it as meant to be committed.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.