How can I change the version of npm using nvm?

node.jsNpm

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

npm install npm@x.x.x -g
npm install npm@5.4.0 -g

Solution 3 - node.js

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.

Solution 4 - node.js

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

Solution 5 - node.js

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.

Solution 6 - node.js

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

Solution 7 - node.js

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/[email protected] /usr/local/bin/npm

Solution 8 - node.js

  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

Solution 9 - node.js

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

Solution 10 - node.js

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

Solution 11 - node.js

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

Solution 12 - node.js

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

npm install -g [email protected]
rm /usr/local/bin/npm
ln -s ~/.npm-packages/bin/npm /usr/local/bin/npm
npm --version

Solution 13 - node.js

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

Solution 14 - node.js

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

Solution 15 - node.js

For some reason npm install -g [email protected] 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)

Solution 16 - node.js

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.

Solution 17 - node.js

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)

Solution 18 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthisissamiView Question on Stackoverflow
Solution 1 - node.jslawrenceView Answer on Stackoverflow
Solution 2 - node.jsOckoView Answer on Stackoverflow
Solution 3 - node.jsfijiaaronView Answer on Stackoverflow
Solution 4 - node.jssamlandfriedView Answer on Stackoverflow
Solution 5 - node.jsRyan ShillingtonView Answer on Stackoverflow
Solution 6 - node.jsadnan2ndView Answer on Stackoverflow
Solution 7 - node.jsArun ReddyView Answer on Stackoverflow
Solution 8 - node.jsAlvin SmithView Answer on Stackoverflow
Solution 9 - node.jsjimView Answer on Stackoverflow
Solution 10 - node.jsDeepak ChandhView Answer on Stackoverflow
Solution 11 - node.jsDimitri ReifschneiderView Answer on Stackoverflow
Solution 12 - node.jsNick GrealyView Answer on Stackoverflow
Solution 13 - node.jsWelcomeToView Answer on Stackoverflow
Solution 14 - node.jsSyed HaseebView Answer on Stackoverflow
Solution 15 - node.jsAndrey GritsayView Answer on Stackoverflow
Solution 16 - node.jswortwartView Answer on Stackoverflow
Solution 17 - node.jshighjumpView Answer on Stackoverflow
Solution 18 - node.jsShrinivas KalangutkarView Answer on Stackoverflow