thisissami
thisissami

Reputation: 16383

How can I change the version of npm using nvm?

I've been using NVM to install the latest versions of Node.js for my Node.js 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.js binary. However, there doesn't seem to be a 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, is there a better way to go about doing this?

Upvotes: 576

Views: 981720

Answers (22)

fijiaaron
fijiaaron

Reputation: 5185

For Node 0.4 or lower: nvm doesn't handle npm.

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

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

npm install nvm
. ~/nvm/nvm.sh

Install Node.js 0.4.x with nvm:

nvm install v0.4.12
nvm use v0.4.12

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

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

This will detect Node.js 0.4.12 and install npm 1.0.106 in your ~/nvm/v0.4.12/lib/node_modules folder and create a symbolic link 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.

Upvotes: 63

Ocko
Ocko

Reputation: 2021

Npm can install itself. Just use npm install with the global flag -g, to overwrite the version of npm currently installed.

For example:

npm install [email protected] -g

Where x.x.x represents the version you want. E.g.

npm install [email protected] -g

Upvotes: 189

Allan Elder
Allan Elder

Reputation: 4104

So I just went through this as npm was throwing an "unsafe legacy renegotiation" error using v18. Node v17 and higher, OpenSSL has been updated to v3.0.7. SSL_OP_LEGACY_SERVER_CONNECT is the option that went from enabled by default in OpenSSL 1.1.1 to disabled by default in OpenSSL 3.0

To downgrade NPM via NPM:

nvm install v16.20.0 nvm use v16.20.0

Upvotes: 0

nikitaeverywhere
nikitaeverywhere

Reputation: 1353

If you need it temporarily, you can use npm bundled with nvm-downloaded node version directly.

For example, this shows where is the current node executable and then runs npm install:

$ whereis node
node: ~/.nvm/versions/node/v18.12.1/bin/node
$ ~/.nvm/versions/node/v18.12.1/bin/npm -v
8.19.2
$ ~/.nvm/versions/node/v18.12.1/bin/npm install
...

Upvotes: 0

Lorka
Lorka

Reputation: 69

To resolve:

  • I installed the latest version of node using nvm for Windows, which also installed the latest version of npm.
  • I deleted the npm directory from the node_modules folder and replaced it with the new version from the latest node install.

This allowed me to use the latest version of npm with a previous version of node, and resolved the issue I was having.

Upvotes: 1

João Saraiva
João Saraiva

Reputation: 1

In order to change your npm version when using nvm, you must install your npm version only inside the folder of the desired Node.js version. For instance, if you want to install npm version 8.12.1, you must do it inside the node version folder.

If you do it globally, it will not work.

Upvotes: 0

highjump
highjump

Reputation: 3075

You can install two versions of Node.js using nvm, and install different version of npm on each Node.js environment.

For example, nvm install 14.18.0 and nvm install 14.18.1 will install two separate Node.js 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 to different npm versions without pain.

nvm use 14.18.0

    Now using node v14.18.0 (npm v6.14.15)

nvm use 14.18.1

    Now using node v14.18.1 (npm v7.24.2)

Upvotes: 16

wortwart
wortwart

Reputation: 3360

In my case I updated npm from version 6 to 8 in a Node.js environment set to version 10 by nvm. This resulted in npm not working any more, 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.

Upvotes: 4

Deepak Chandh
Deepak Chandh

Reputation: 275

We can easily solve this using n.

To install n:

npm install -g n

To switch versions:

n latest

To switch to a particular version:

n 10.16.0

Upvotes: 8

Nick Grealy
Nick Grealy

Reputation: 25942

A slight variation on the previous instructions. It worked for me (macOS v10.12.6 (Sierra)).

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

Upvotes: 3

adnan2nd
adnan2nd

Reputation: 2211

In Windows, run your terminal as system administrator (in case there are permission issues as I had). Then use a specific Node.js version (say 7.8.0) by

nvm use 7.8.0

Then update your npm to desired the specific version by:

npm install -g [email protected]

Upvotes: 42

Andrey Gritsay
Andrey Gritsay

Reputation: 990

For some reason, npm install -g [email protected] didn't work, so I've changed the version of npm in file package.json in the npm folder:

which npm

/opt/homebrew/bin/npm

And then run

npm install npm

Platform: macOS v11.2.2 (Big Sur) with M1 (arm64).

Upvotes: 0

Syed Haseeb
Syed Haseeb

Reputation: 312

The easy way to change version is first to check your available version using

nvm ls

Then select a version from the list

nvm use version

Upvotes: 3

Alvin Smith
Alvin Smith

Reputation: 634

  1. Find the Node.js and npm version you want to use from here Previous Releases
  2. nvm use 8.11.4
  3. You already got the npm 5.6 with Node.js 8.11.4

Just go with nvm use node_version.

Upvotes: 11

Shrinivas Kalangutkar
Shrinivas Kalangutkar

Reputation: 1009

NVM installation and usage on Windows

Below are the steps for NVM installation on Windows:

NVM stands for Node Version Manager, which will help to switch your Node.js versions for specific use. It also allows the user to work with multiple npm and Node.js versions.

  • Install nvm setup.
  • Use command "nvm list" to check the list of installed Node.js versions.
  • Type "nvm use version number[6.9.3]" to switch versions.
  • For more information.

Upvotes: -1

WelcomeTo
WelcomeTo

Reputation: 20591

I had the same issue after installing nvm-windows on top of an existing Node.js installation. The 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

Upvotes: 4

Ryan Shillington
Ryan Shillington

Reputation: 25207

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.

Upvotes: 51

Dimitri Reifschneider
Dimitri Reifschneider

Reputation: 467

Use npm i -g npm. Try to run this as well.

Upvotes: 3

jim
jim

Reputation: 111

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 version they have marked as latest.

Upvotes: 8

Arun Reddy
Arun Reddy

Reputation: 3733

Changing npm versions on Linux-based OSes isn't a straightforward one-command process yet. I have done the following to switch back to older versions 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 [email protected]

Remove the symbolic link in /usr/local/bin/:

sudo rm /usr/local/bin/npm

Recreate the symbolic link using the desired version of npm you have installed:

sudo ln -s /usr/bin/[email protected] /usr/local/bin/npm

Upvotes: 22

Lawrence
Lawrence

Reputation: 10780

As noted in another 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.js version.

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

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

For later versions of npm it is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.

I installed Node.js 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.


In a newer version, npm -g is smart and installs modules into the path above instead of the system global path.

Upvotes: 617

samlandfried
samlandfried

Reputation: 821

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

Upvotes: 49

Related Questions