Jump to content

How to update npm using nvm

Posted in JavaScript · 1 minute read

If you are JavaScript developer and not using nvm you might want to look at it. Node Version Manager is easy way to install, manage and work with multiple node versions.

Recently I got npm update notification, that looks something like this:

npm update notification

For some reason, when I had node installed through nvm, npm i -g npm didn't work. However, when I was using a "system" node version, installed from Node.js website, it did. I can't remember the exact error, but it was probably something with my PATH configuration.

So I tried to find a path where global packages are installed when using nvm. Once I found it, it was super easy to update npm in that specific folder.

# navigate to nvm's node lib folder
# (replace v8.4.0 with your version)
cd ~/.nvm/versions/node/v8.4.0/lib/

# update npm right there
npm install npm

# reopen your terminal

That's it. Great thing is that npm can update itself. And this works for any other global npm package.

Or, as suggested in the comments, just use nvm's command:

nvm install-latest-npm

Comments (8)

7. November 2020
Stanko

Hey Jordan,

First I want to thank you for working on nvm, it is a life saver!

I encountered this issue with updating npm while using nvm in 2017, and npm i -g npm didn't work for some reason. However, it did work when I had a single node version installed from Node.js website. And I remember seeing quite a few people with the same problem on stack overflow.

That is why I wrongly accused nvm, and I'm sorry for that, I will update the post to reflect this.

Cheers!

7. November 2020
Jordan Harband

Hi! I maintain nvm, and "nvm doesn’t let you update only npm" is utterly incorrect. nvm does not have any impact on your ability to update npm, except that it removes the need to use sudo.

npm install -g npm always works, and if it doesn't work for you, please file an issue on nvm's repo, because you'll be the first person on the planet to report it.

28. January 2020
Élisson Michael

Thank You for Sharing This.

25. January 2020
Zafer

Great tip. Thanks.

14. December 2019
C. L. Jones

Nice find, Shawn! Exactly what I needed :).

23. October 2018
Stanko

Heh that is awesome, thanks Shawn!

23. October 2018
Shawn Erquhart

Turns out nvm does have a command for this (nvm/issues/1541#issuecomment-377826880):

nvm install-latest-npm

Not sure why it isn't documented.

17. June 2018
Ethan

Easier way is simply to do an npm global install, which would automatically use the correct path to the current nvm version in use.

npm install -g npm