How do you update npm to the latest version? [duplicate]

Solution 1:

I still don't understand why, but I have to run npm install -g npm twice for it to have the desired effect:

vagrant@box:~$ npm -v
1.3.10
vagrant@box:~$ sudo npm install -g npm
npm http GET https://registry.npmjs.org/npm
npm http 200 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/-/npm-2.1.12.tgz
npm http 200 https://registry.npmjs.org/npm/-/npm-2.1.12.tgz
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
[email protected] /usr/local/lib/node_modules/npm
vagrant@box:~$ npm -v
1.3.10
vagrant@box:~$ sudo npm install -g npm
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
[email protected] /usr/lib/node_modules/npm
vagrant@box:~$ npm -v
2.1.12

Solution 2:

When you first do npm install -g npm, the shell (Bash) will search for npm in your path, find /usr/bin/npm installed by the system package, and then use it to install the new version of npm. The new version will be installed in /usr/local/bin/npm.

Now, your path should have /usr/local/bin/ BEFORE /usr/bin/, so you would think it would now pick up the updated version in /usr/local/bin/, right? Wrong.

Bash will CACHE executable paths after the first time it searches for them, so when you say npm the 2nd time, it is still using the cached version which it first found as /usr/bin/npm.

To tell Bash to clear this cache and look through the path again, you have to do a hash -r.

After installing npm and doing this, my shell picked up the new version of npm just fine.

Thanks

Solution 3:

You can update nodejs by using npm itself, a PPA, or manually.

npm:

Check the current version you have:

node -v

The following clears your cache.

sudo npm cache clean -f

Install n

sudo npm install -g n

You can tell it to install a specific version like so:

sudo n 0.8.11

Or just tell it to install the latest stable version. Both may take a while.

sudo n stable

To see if it actually upgraded, run:

node -v

PPA:

Other option is to install it via a PPA by chris-lea;

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs

This PPA Supports the following distros: Utopic (14.10), Trusty (14.04), Saucy (13.10), Raring (13.04), Quantal (12.10), Precise (12.04), Oneiric (11.10), Natty (11.04), Lucid (10.04).

Manually:

You can always update it by manually downloading the latest version and installing it yourself!


Reference:

  • Upgrade Node.js via NPM

  • Node.js fundamentals: how to upgrade the Node.js version | The Holmes Office

  • node.js : chris lea

  • node.js - downloads

Solution 4:

Update NPM to latest version in one command

To upgrade or update the version of your npm, just type in terminal:

sudo npm install npm@latest -g

As mentioned in the footer of the NPM documentation