40

Is there a way to undo the command npm update? I ran the command and now I have a bunch of unmet dependencies and some errors.

I'm trying to manually reinstall and fix all the errors but does anyone know how to essentially revert an npm update?

aug
  • 11,138
  • 9
  • 72
  • 93
  • 1
    Related: [Why does npm install say I have unmet dependencies?](http://stackoverflow.com/questions/20764881/why-does-npm-install-say-i-have-unmet-dependencies) – Jonathan Lonowski Jan 14 '15 at 23:40
  • @JonathanLonowski Thanks I actually ended up doing the solution suggested in that question. Wish I saw it sooner. – aug Jan 15 '15 at 00:08
  • I’m horrified at the implication that you aren’t using version control (like `git`). Trust me, you want this. – Alan H. Jun 21 '18 at 05:53
  • @AlanH. yeah when I originally posted this answer, I wasn't super familiar with how npm worked internally. [The documentation is a lot clearer now](https://docs.npmjs.com/cli/update). Also before `npm@5.0.0` when npm didn't save the dependency changes to your package.json, it wasn't as obvious what was changing – aug Jun 21 '18 at 17:02
  • 1
    Got it Consider Yarn, too. It’s a better-designed and more reliable `npm` in our experience. – Alan H. Jun 21 '18 at 20:44

5 Answers5

29

You can restore your package.json file to its previous state (hopefully you still have that, or at least remember what you had changed), and then do another npm update.

[UPDATE]

However, in general, this technique does not guarantee that your entire dependency tree will be restored to its exact former state (since the package.json files in a dependency tree often loosely specify dependency versions).

If you need to ensure that your package's dependency tree can be restored exactly, you have to use something like npm shrinkwrap to "lock down" the dependency versions (before you publish the package).

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 4
    Wouldn't this simply be redoing the `npm update` that I first did? I wanted to revert back to my original state, not `npm update` again. – aug Jan 15 '15 at 00:10
  • 3
    `npm update` enforces your node package's dependencies, exactly as specified in your `package.json` (including the versions of the dependencies). It does not simply update everything to the latest version. – cybersam Jan 15 '15 at 00:13
  • Ah I see thank you for the clarification. Accepting your answer. – aug Jan 15 '15 at 00:16
  • 3
    What do you do if your `package.json` has tilde or caret versions and a dependency update (non-major) breaks your build? How do you revert to the exact versions you had installed before the update? – Jay Sep 19 '16 at 21:10
  • I have updated my answer. Unfortunately, there is probably nothing you can do at this point to easily fix this issue. Going forward, you may want to use `npm shrinkwrap` to avoid this issue. – cybersam Sep 19 '16 at 21:28
  • In my experience, `rm -rf node_modules && npm update` does the trick nicely. So well in fact I am considering putting it in my build script. If it was not for the ~10s it adds to my build and the fact that internet access would become mandatory to build. – pieroxy Jan 11 '22 at 16:23
16

Do this:

git log

then Copy the latest latest key. This will let you checkout your latest commit.

git checkout "your-key" package.json
git checkout "your-key" package-lock.json 

without the " quotes

(package-lock.json is really necessary, but I like to do et anyway - "Just to make sure...")

you can do a

git status

to make sure that your package.json and package.lock

You can also do the

rm -rf node_modules/ 

which will delete node_modules folder

npm install
wscourge
  • 10,657
  • 14
  • 59
  • 80
  • 1
    if you want the latest commit you can do `HEAD` or if you've already committed changes just do `HEAD~1` to get the commit before. Thanks for the follow up answer though :) I'm sure some people will appreciate it – aug Jun 21 '18 at 17:05
  • I don't know whether it makes any difference but at the time of writing `npm install` seems to downgrade correctly even if you don't wipe out `node_modules`. – Álvaro González Aug 02 '19 at 18:49
3

All I did was to do "rm -rf node_modules" to uninstall the updated node_modules and then "npm install" to reinstall them; my package.json hadn't change when I did npm update which caused all the havoc. So by deleting and reinstalling node_modules I'm gladly back in business.

Pablo
  • 1,571
  • 3
  • 11
  • 11
  • But `npm update` does write to *package.json*... Probably in earlier versions it would not but right now I see my *package.json* being updated everytime I update. – adelriosantiago Mar 23 '19 at 09:08
0

I've learned to use different branches in git, even when working solo.

So when I recently screwed up my npm, I could just:

git stash (stashes recent changes) git switch main (switches back to main branch) npm update (re-aligns npm packages with package.json file)

The key is to get back to the file, and npm update. Or else pull the file out of github, and replace yours, and then npm update.

-1
git checkout "your-key" package.json
git checkout "your-key" package-lock.json 

without the " quotes

(package-lock.json isn't really necessary, but I like to do it anyway - "Just to make sure...")

You can do a

git status
David Buck
  • 3,752
  • 35
  • 31
  • 35
Palak Mantry
  • 59
  • 1
  • 4