22

I am using npm v5.3.0. Now I want to update npm to v5.4.1.To do this I type the following in my terminal:

npm install -g npm

without any errors.However when I type npm -v the version I get is the 5.3.0 not the 5.4.1

I have to mention that I have changed the npm prefix to another directory. Any ideas? I use Ubuntu 17.04

That's my console output : enter image description here

Manos Kounelakis
  • 2,848
  • 5
  • 31
  • 55

8 Answers8

20

I solved it on Ubuntu 18.04 LTS by switching to root using sudo su then I run the following

curl -L https://www.npmjs.com/install.sh | sh
Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    I'm using Ubuntu 18.04 LTS too, really nothing has worked except this ! – shamaseen Mar 20 '19 at 08:39
  • I noticed that `npm i -g npm` updates npm installed in `~/.npm` directory. This script works because it updates npm installed in `/usr/local/` directory. You can check yourself what path npm command is using by looking at `which npm` output. – naXa stands with Ukraine Apr 07 '19 at 10:00
5

The problem was that I has set the PATH variable in my ~/.profile wrong. To help you understand.I had changed npm prefix to ~/.npm-global

However I set path to ~/npm-global.So I fixed the PATH variable in my ~/.profile to point to ~/.npm-global instead.

Manos Kounelakis
  • 2,848
  • 5
  • 31
  • 55
4

Please try this for the latest (5.4.1) version:

npm install -g npm@latest

or for any specific version, for e.g.:

npm install -g npm@5.4.1

If that doesn't help, go to where you have npm installed and remove the files: npm, npm.cmd.

After that please try to npm install -g npm@5.4.1 again.

Maciej Jureczko
  • 1,560
  • 6
  • 19
  • 23
2

I had to dig a little bit more (probably I have some misconfig) and take an hardcore approach... First I ran which npm to get the script that was running, that was /usr/local/opt/node@8/bin/npm.js. From that script I saw that the npm package was actually in /usr/local/opt/node@8/lib/node_modules/npm. So I ran

cd /usr/local/opt/node@8/lib/
npm i npm@6.11.2 # or your desired version
rm package-lock.json # clean up

And that did the trick, hope it helps someone :)

NB: I'm not saying that you should do this right away, first is better if you try the other approaches but if other approaches are not working give it a shot

Silvio Biasiol
  • 856
  • 8
  • 14
1

In my case (Ubuntu) the only thing worked was reboot.

After

npm install npm@latest -g

The output of

npm --version

was not changed (showing old version).

Unless I did a reboot

Syscall
  • 19,327
  • 10
  • 37
  • 52
Anar Salimkhanov
  • 729
  • 10
  • 12
0

To upgrade, run: sudo npm install npm@latest -g

The npm module is available for download at https://registry.npmjs.org/npm/-/npm-{VERSION}.tgz.

Azoulay Jason
  • 2,787
  • 5
  • 21
  • 46
0

I was facing the same error today. I updated node. Since npm comes bundled with node, npm was also updated but updated npm version was not reflecting. I, also, tried to update npm version by using commands npm install -g npm@latest. But, the version was not reflecting.

This is how I resolved it.

  1. A lot of post suggested to use the command npm cache clean --f. But, it did not work for me. Hence, I deleted the "npm" and "npm-cache" folders from the location C:\Users\SearchYourUserName\AppData\Roaming

  2. Uninstalled again the existing node.

  3. Re-Installed node version v10.x.x. I downloaded the node-v10.22.1-x64.msi file from https://nodejs.org/dist/latest-v10.x/ and installed it.

Then checked the npm version. It had worked.

0

I solved this by navigating in an admin command prompt to C:/Users//AppData/Roaming and then with rd /s /q (delete) the npm and npm cache folders. After that I am now able to update to the latest npm version of 9.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 21 '23 at 13:29