399

I've been using NVM to install the latest versions of nodeJS for my node work. It works totally fine for installing separate versions and switching between them. It also installs the latest version of NPM within each local .../bin folder along with the node binary. However, there doesn't seem to be any way to switch the version of NPM that I'm using (or at least I can't figure it out).

The only solution I can think of myself is to delete the binary that it's defaulting to (which is the NPM that was installed when I first installed node with NVM), and in its place to put the latest NPM binary. However, I'm wondering if there are any better ways to go about doing this.

bdesham
  • 14,616
  • 11
  • 78
  • 119
thisissami
  • 14,145
  • 15
  • 45
  • 71
  • 4
    Why do you wanna change the version of NPM, it's not like you gonna install any package that only works with an old version of NPM. And after node 0.6, node comes with the latest NPM. So changing node version will also change npm version. – Farid Nouri Neshat Mar 18 '12 at 04:35
  • 2
    except it doesn't through NVM. yes - node comes with NPM, but that just means that it installs a binary alongside it. i can see both binaries in their respective folders, but when i run "nvm use v0.6.13", the npm binary in use remains the one that was installed with the older version of node, instead of the one installed with the newest version. – thisissami Mar 19 '12 at 15:35
  • 2
    Here's the code for npm: https://github.com/creationix/nvm/blob/master/nvm.sh#L135-148 , which is not needed since, 0.6, so maybe you should try to remove that part. That should help. You can also create an issue on github. – Farid Nouri Neshat Mar 20 '12 at 02:35
  • I am working on this problem right now, any help appreciated - https://stackoverflow.com/questions/50976138/tool-to-change-npm-version-for-testing-by-symlinking-different-versions-to-glo – Alexander Mills Jun 21 '18 at 19:25

18 Answers18

478

EDIT: several years since this question was first answered, as noted in a newer answer, there is now a command for this:

nvm now has a command to update npm. It's nvm install-latest-npm or nvm install --latest-npm.

nvm install-latest-npm: Attempt to upgrade to the latest working npm on the current node version

nvm install --latest-npm: After installing, attempt to upgrade to the latest working npm on the given node version

Below are previous revisions of the correct answer to this question.

Over three years after this question was first asked, it seems like the answer is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.

I just installed node 4.2.2, which comes with npm 2.14.7, but I want to use npm 3. So I did:

cd ~/.nvm/versions/node/v4.2.2/lib
npm install npm

Easy!

And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node.


EDIT 1: In the newest version, npm -g is smart and installs modules into the path above instead of the system global path.


Thanks @philraj for pointing this out in a comment.

Kevin Burke
  • 55,512
  • 71
  • 177
  • 289
lawrence
  • 9,239
  • 5
  • 37
  • 50
  • 16
    and for those who don't know where their nvm is, like me, run a `which npm`. – Larry Jan 27 '16 at 15:33
  • 32
    And if you want a version of npm other than the latest, run `npm install npm@major.minor.patch` to get that specific version, e.g. `npm@3.10.9` – Michael Nov 21 '16 at 18:15
  • 2
    Something similar works for [nvm-windows](https://github.com/coreybutler/nvm-windows/) too, e.g. `cd ~/AppData/Roaming/nvm/v5.10.1`. – absynce Feb 24 '17 at 20:17
  • For those wanting a use case, I was tasked with getting a legacy project running which needed Node 5 and NPM 2, the latter being required because of the change in how [peerDependencies](https://docs.npmjs.com/files/package.json#peerdependencies) are handled. NPM versions 1 and 2 will automatically install peerDependencies if they are not explicitly depended upon higher in the dependency tree, whereas this is no longer the case. The legacy app build process relied on this older behavior. – spex Mar 17 '17 at 19:13
  • 3
    For anyone using nvm for Windows, the same can be done as above (running npm install npm), but in the node install locations for Windows. In Windows 10, this is ~/AppData/Roaming/nvm/[your-version]. i.e Users\myusername\AppData\Roaming\nvm\v9.7.1 – Sean Holmesby Mar 28 '18 at 16:31
  • !! Is there a way to switch npm versions with nvm without installing a new/different npm version each time you want to switch? – Alexander Mills Jun 21 '18 at 02:50
  • I am working on this problem right now, any help appreciated - https://stackoverflow.com/questions/50976138/tool-to-change-npm-version-for-testing-by-symlinking-different-versions-to-glo – Alexander Mills Jun 21 '18 at 19:25
  • 3
    Even easier: `nvm install-latest-npm` as answered below by samlandfried -https://stackoverflow.com/a/47519162/5991278 – philraj Aug 17 '18 at 19:07
  • 2
    This isn't a comprehensive answer to the question even though there are good comments. The question asks about changing npm while the answer only deals with upgrading npm. I have a legacy project that needs an old version of node which I can get using nvm but the associated version of npm is too new. Unfortunately Michael's upvoted comment doesn't work for me and it looks like work is needed on the existing files/symlinks. However, I had success with Ryan Shillington's answer by replacing latest with 5.6.0. – Astrophe Oct 16 '20 at 08:50
  • Make sure to update nvm to the latest version before running `nvm install-latest-npm`. I had an old version of nvm and the option install-latest-npm did not exist. After updating, the option was available. – mrbinky3000 Jan 03 '22 at 21:57
  • This doesn't work for `nvm-windows`, check this - https://stackoverflow.com/questions/54652381/updating-npm-when-using-nvm-windows – kaushalpranav Jan 07 '22 at 12:16
97
npm install npm@x.x.x -g
npm install npm@5.4.0 -g
Ocko
  • 1,093
  • 7
  • 4
  • 13
    Though the question is about nvm, I actually came here for that, thanks. Upvoting. – Mouloud88 May 16 '18 at 12:06
  • 2
    This command will work regardless of which version of Node you've installed with nvm, because of the `-g` flag. When you switch versions in nvm, the npm version will change as well, so it's easy to undo this action. – A.Wan Nov 24 '21 at 00:26
51

nvm doesn't handle npm.

So if you want to install node 0.4.x (which many packages still depend on) and use NPM, you can still use npm 1.0.x.

Install node 0.6.x (which comes with npm 1.1.x) and install nvm with npm:

npm install nvm
. ~/nvm/nvm.sh

Install node 0.4.x with nvm:

nvm install v0.4.12
nvm use v0.4.12

Install npm using install.sh (note the -L param to follow any redirects):

curl -L https://npmjs.org/install.sh | sh

This will detect node 0.4.12 and install npm 1.0.106 in your ~/nvm/v0.4.12/lib/node_modules folder and create symlink for nvm

~/nvm/v0.4.12/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js

If you try to run npm, it will still give an error but if you do nvm use v0.4.12 again, it should now work.

seangates
  • 1,416
  • 11
  • 28
fijiaaron
  • 4,725
  • 3
  • 34
  • 28
47

nvm now has a command to update npm. It's nvm install-latest-npm or npm install --latest-npm.

samlandfried
  • 738
  • 6
  • 11
46

I'm on Windows and I couldn't get any of this stuff to work. I kept getting errors about files being in the way. This worked though:

cd %APPDATA%\nvm\v8.10.0           # or whatever version you're using
mv npm npm-old
mv npm.cmd npm-old.cmd
cd node_modules\
mv npm npm-old
cd npm-old\bin
node npm-cli.js i -g npm@latest

cd %APPDATA%\nvm\v8.10.0 # or whatever version you're using
rm npm-old
rm npm-old.cmd
cd node_modules\
rm -rf npm-old

And boom, I'm back in business.

Ryan Shillington
  • 19,533
  • 12
  • 88
  • 99
  • 3
    Thanks, this worked for me as well, on Windows. (had same issue of file conflicts) – Venryx Jul 20 '18 at 04:53
  • 1
    Worked for me thx. Am using nvm on Windows to manage node and wanted to uplift nvm after switching to node version. Had to run console with elevated permissions. My path nodejs folder is "C:\Program Files\nodejs" which is an alias for a.n.other location on my machine nvm uses to cache downloads (not APPDATA), guess I elected a different location when installing nvm. – Moika Turns Mar 12 '19 at 13:11
  • 2
    Thank you, they probably need to fix the way npm update on every old nodejs version on nvm windows. – rbinsztock May 02 '19 at 08:36
  • 1
    That helped me to update npm which came with Node 10.x. Thank you! – andypotato Sep 10 '19 at 12:01
  • 2
    Worked on windows with nodejs v10.15.3, had to change `mv` to `ren` and `rm` to `del` and `rmdir` for me. – deerchao Sep 19 '19 at 04:35
  • 2
    I found that when upgrading from npm@6, I also had to rename `npx` and `npx.cmd`. Not sure what version introduced npx. – Ken Lyon Feb 19 '21 at 23:17
  • With v5.11.0 I get this error: module.js:341 throw err; ^ ''' Error: Cannot find module 'process-nextick-args' at Function.Module._resolveFilename (module.js:339:15) at Function.Module._load (module.js:290:25) at Module.require (module.js:367:17) ... at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) ''' – Fantantonio Oct 09 '21 at 09:12
19

In windows, run your terminal as admin (in case there are permission issues as I had). Then use a specific node version (say 7.8.0) by

nvm use 7.8.0

then update your npm to desired specific version by

npm install -g npm@5.0.3
adnan2nd
  • 1,735
  • 17
  • 16
18

Changing npm versions on linux based OSs isn't a straight forward one command process yet. I have done following to switch back to older version of npm. This should work to get any version of npm working. First install the version of npm you want to use:

sudo npm install -g npm@X.X.X

Remove the sym link in /usr/local/bin/

sudo rm /usr/local/bin/npm

Recreate the sym link using the desired version of npm you have installed

sudo ln -s /usr/bin/npm@X.X.X /usr/local/bin/npm
Arun Reddy
  • 2,883
  • 1
  • 19
  • 16
  • 1
    followed these instructions trying to go from 2.14.7 to 2.15.9, and now when I type npm -v, I get 3.8.6. Dammit. – Kyle Baker Nov 18 '16 at 16:52
  • 3
    After trying several things, for me, just using `sudo npm install -g npm@x.x.x` gets me that version responding from `npm -v`. This at least worked going from 4.x to 2.15.9, anyways. – Kyle Baker Nov 18 '16 at 17:43
  • 1
    In my case `sudo npm install -g npm@4.6.1` only installed 4.6.1 but npm 5.1.0 was still used. Your tip help me switch to 4.6.1. – Hung Tran Jul 11 '17 at 04:11
10
  1. find the node and npm version you want to use from here https://nodejs.org/en/download/releases/
  2. nvm use 8.11.4
  3. you already got the npm 5.6 with node 8.11.4

Just go with nvm use node_version

Alvin Smith
  • 439
  • 4
  • 8
  • 1
    That works fine for the first time. If you upgrade run `npm i -g npm` from an older node version and it updates to latest, your npm version will be the latest. – Jonas Merhej Jan 13 '22 at 14:16
8

By looking at www.npmjs.com/install.sh I found there is a way to install a specific version by setting an environment-variable

export npm_install="2.14.14"

Then run the download-script as described at npmjs.com:

curl -L https://www.npmjs.com/install.sh | sh

If you omit setting the npm_install variable, then it will install the the version they have marked as latest

jim
  • 91
  • 1
  • 4
  • 2
    I had to use `curl -L https://www.npmjs.com/install.sh | sudo sh`, aaaaand this method did not work, getting me the latest version even though I had exported said environment variable. God dammit. I wanted version 2.15.9 from 2.14.7, answer below put me at 3.8.6, and now this answer puts me at 4.0.2. WTF. – Kyle Baker Nov 18 '16 at 17:11
5

We can easily solve this using n.

To install n

>> npm install -g n

To switch versions

>> n latest

To switch to particular version

>> n 10.16.0
Deepak Chandh
  • 111
  • 2
  • 7
3

What about npm i -g npm? Did you try to run this as well?

2

Slight variation on the above instructions, worked for me. (MacOS Sierra 10.12.6)

npm install -g npm@3.10.10
rm /usr/local/bin/npm
ln -s ~/.npm-packages/bin/npm /usr/local/bin/npm
npm --version
Nick Grealy
  • 21,434
  • 9
  • 96
  • 110
2

I had same issue after installing nvm-windows on top of existing Node installation. Solution was just to follow the instructions:

You should also delete the existing npm install location (e.g. "C:\Users\AppData\Roaming\npm") so that the nvm install location will be correctly used instead.

Installation & Upgrades

WelcomeTo
  • 18,613
  • 51
  • 157
  • 276
0

The easy way to change version is first to check your available version using nvm ls then select version from the list nvm use version

Syed Haseeb
  • 166
  • 1
  • 9
0

For some reason npm install -g npm@X.X.X didn't work, so I've changed version of npm in package.json in npm folder

➜  ~ which npm
/opt/homebrew/bin/npm

and then run

npm install npm

macOS 11.2.2, M1 (arm64)

Andrey Gritsay
  • 726
  • 1
  • 7
  • 15
0

In my case I updated npm from version 6 to 8 in a node environment set to version 10 by nvm. This resulted in npm not working anymore, rendering the answers I found here useless.

I finally resorted to clearing the nvm folder:

rm -rf ~/.nvm/versions/node/*

Afterwards I could work with nvm and npm again. My lesson: Never install a fresh npm version with npm. nvm install-latest-npm seems to let you safely update within legacy environments.

wortwart
  • 2,822
  • 22
  • 29
0

You can install two versions of node using nvm, and install different version of npm on each node environment.
For example, nvm install 14.18.0 and nvm install 14.18.1 will install two separate node environments.

In 14.18.0, you can use npm v6 (installed by default).
In 14.18.1, you can use npm v7 (install with npm install -g npm@7).

This way you can switch different npm version without pain.

high@highs-MacBook-Pro / % nvm use 14.18.0
Now using node v14.18.0 (npm v6.14.15)
high@highs-MacBook-Pro / % nvm use 14.18.1
Now using node v14.18.1 (npm v7.24.2)
highjump
  • 1,837
  • 2
  • 8
  • 15
-1

NVM Installation & usage on Windows

Below are the steps for NVM Installation on Windows:

NVM stands for node version manager, which will help to switch the your node versions for specific use. It also allows the user to work with multiple npm and node versions.

Install nvm setup. Use command "nvm list" to check list ofinstalled node version. Type "nvm use version number[6.9.3]" to switch versions. For more info