npm audit fix broke the build and you want your old dependencies back
npm audit fix --force installed major versions and the app no longer buildsShort 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.
Fix it fast
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.json2Match what you see, then jump to its fix
- 1
git status shows package-lock.json modified, package.json unchangedThe fix only changed the lockfileFix - 2
git status shows package.json modified as well, with higher version ranges--force moved dependencies to new major versionsFix - 3
git status is clean, but git log shows the dependency update commitThe audit fix was already committedFix
- 1
What does the error look like?
npm audit fix --force installed major versions and the app no longer buildsgit 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.json2Fix
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 ci3Verify
git reports no change to the lockfile and the build passes again.
git status --short
npm run buildConfirmed by docs.npmjs.com
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.json2Fix
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-run3Verify
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=0Confirmed by docs.npmjs.com
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.json2Fix
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 ci3Verify
The diff against the earlier commit is empty for both files.
git diff HEAD~1 -- package.json package-lock.jsonConfirmed by git-scm.com
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
npm audit fix runs, but the vulnerabilities are still listed
To address issues that do not require attention, run: npm audit fix
npm audit fix only installs versions that stay inside your declared version ranges.
3 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix