Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update npm? #1541

Closed
danielcobo opened this issue Jun 1, 2017 · 26 comments
Closed

How to update npm? #1541

danielcobo opened this issue Jun 1, 2017 · 26 comments

Comments

@danielcobo
Copy link

  1. What is the "correct way" to update NPM when using nvm?
  2. Also.. is it possible to use yarn? If so, again, what would be the best way?

Thank in advance

@ljharb
Copy link
Member

ljharb commented Jun 1, 2017

nvm manages node, not npm. npm install -g npm is how you update npm, with or without nvm.

You can use yarn, if you like - npm install -g yarn. Just like you would without using nvm.

@ljharb ljharb closed this as completed Jun 1, 2017
@jericopulvera
Copy link

nvm install-latest-npm

  • Attempt to upgrade to the latest working npm on the current node version

@matteplus
Copy link

This worked for me: https://stanko.github.io/nvm-updating-npm/

@ljharb
Copy link
Member

ljharb commented Apr 18, 2018

Those instructions are a bit silly; you can npm install -g npm from anywhere.

@matteplus
Copy link

@ljharb I ran npm install -g npm many times, and always worked fine. But the next time npm would suggest an update anyway. Turned out that npm install -g npm was updating /usr/bin/npm, not ~/.nvm/versions/node//bin/npm

@ljharb
Copy link
Member

ljharb commented Apr 18, 2018

@matteplus that command needs to be run while nvm using the proper node version; in other words, it will update whichever which npm is, which is expected. You don’t want to be using your system npm to update an nvm-managed npm.

@matteplus
Copy link

@ljharb I see your point and that is exactly what I expected, but not what I found. which npm always returned the nvm-managed instance. It looks like the opposite was happening: the nvm-managed npm was updating the system npm, not itself. Most likely there is/was something oddly configured on my side.

@ljharb
Copy link
Member

ljharb commented Apr 18, 2018

That definitely seems strange. Glad you found a workaround, but the proper solution should work for the majority, ideally.

@ljharb
Copy link
Member

ljharb commented Apr 18, 2018

(It follows npm root -g in reality, so maybe you had a prefix set or something)

@matteplus
Copy link

I tried to downgrade and upgrade again and now it works as you described. So yes, possibly something was wrongly set and now it's fixed. Thanks for your responses

@MartinsOnuoha
Copy link

MartinsOnuoha commented Apr 25, 2018

I had this issue today.
However all of the proposed solutions didn't work for me.
running which npm returned /home/iamkratos/.nvm/versions/node/v9.11.1/bin/npm which is clearly the one managed by nvm, running npm i npm show a successful update but when I check with npm -v it returns the older version.

I looked into the nvm directory ~/.nvm/versions/node/v9.11.1/bin and ran ./npm -v now it returns the newer version.
I had to set an alias for npm in my ~/.bashrc. alias npm="~/.nvm/versions/node/v9.11.1/bin/npm"`
It works globally now.

@ljharb
Copy link
Member

ljharb commented Apr 25, 2018

@MartinsOnuoha npm i -g npm - note the -g. The alias should not be required whatsoever.

@vspedr
Copy link

vspedr commented Apr 27, 2018

npm i -g npm is definitely the way to go. Just make sure to open a new terminal session, or else it will still report an older version enven after a successful upgrade.

@ljharb
Copy link
Member

ljharb commented Apr 27, 2018

@vspedr for npm, opening a new terminal session is not required.

@vspedr
Copy link

vspedr commented Apr 27, 2018

@ljharb running npm -v right after npm i -g npm for some reason reports the same version it did before the upgrade. Not that it's a nvm issue, I'm only mentioning it because I lost a few minutes thinking npm did not upgrade successfully when it did

@ljharb
Copy link
Member

ljharb commented Apr 27, 2018

That sounds like a different, troubling issue, and implies your which npm would report the wrong one - in the typical case you shouldn’t have to restart the terminal to reflect changes in npm or node, only in nvm itself.

@black-snow
Copy link

black-snow commented Jan 18, 2019

npm i -g npm
npm ERR! path C:\Program Files\nodejs\npx.cmd
npm ERR! code EEXIST
npm ERR! Refusing to delete C:\Program Files\nodejs\npx.cmd: is outside C:\Program Files\nodejs\node_modules\npm and not a link
npm ERR! File exists: C:\Program Files\nodejs\npx.cmd
npm ERR! Move it away, and try again.

npm proposes to remove npx and npm but then I can't run npm anymore xD

Windows 10
Tried to npm list -g --depth=0 and got:

npm ERR! error in C:\Program Files\nodejs\node_modules\npm\node_modules\pmj.gz: ENOENT: no such file or directory, open 'C:\ProgramData\nvm\v11.6.0\node_modules\npm\node_modules\pmj.gz\package.json'

That's why I tried to npm i npm -g.

@ljharb
Copy link
Member

ljharb commented Jan 18, 2019

when npm is broken, your only recourse is to reinstall node.

@black-snow
Copy link

Just installed 11.7.0 but that doesn't help, the error is the same.
How do we update npm with nvm?

@ljharb
Copy link
Member

ljharb commented Jan 23, 2019

You don’t; npm is updated only with npm itself. You can, though, run nvm install-latest-npm and it will run npm install for you.

@black-snow
Copy link

@ljharb thanks for the reply
There doesn't seem to be a command install-latest-npm. nvm prints it's usage.

@ljharb
Copy link
Member

ljharb commented Jan 23, 2019

You might be running an old version of nvm; try reinstalling the latest.

@Tazzmanian
Copy link

Tazzmanian commented Feb 26, 2019

The only think that worked for me is to go C:\Users\user\AppData\Roaming\nvm\v10.15.1\node_modules\npm and change the version ("version": "6.8.0") and then execute npm install from the current directory and delete the files that were marked as existing on error(only 2 files i think). With npm i -g npm@latest I deleted more files which broke it and then needed to reinstall the node.
On Ubuntu there wasn't any problem just ran npm i -g npm@latest.
Should be working because the number of vulnerabilities is the same as in ubuntu :).

@ljharb
Copy link
Member

ljharb commented Feb 26, 2019

@Tazzmanian on windows, you’d need WSL for this project to work; however, if npm install -g npm doesn’t work then it’s a bug with npm itself.

@Seefer
Copy link

Seefer commented Apr 26, 2019

You might be running an old version of nvm; try reinstalling the latest.

Those using NVM for Windows will struggle to get this npm update feature. I have NVM 1.1.7 which is the latest version on Windows and nvm install-latest-npm does not work :(

@ljharb
Copy link
Member

ljharb commented Apr 26, 2019

@Seefer that’s because that’s not nvm, it’s nvm-windows, an entirely unrelated project. The latest version of nvm is v0.34.0 as of this writing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants