How can I change the version of npm using nvm?
Asked Answered
T

22

540

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?

Tamtama answered 18/3, 2012 at 3:49 Comment(5)
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.Liuka
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.Tamtama
Here's the code for npm: 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.Liuka
I am working on this problem right now, any help appreciated - #50976638Doff
Do not confuse it with nvm-windows (!!!) which is nvm as well in command line!!! Check this instead: #54652881Bellamy
A
583

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.

Arbutus answered 6/11, 2015 at 20:46 Comment(15)
and for those who don't know where their nvm is, like me, run a which npm.Esparza
And if you want a version of npm other than the latest, run npm install [email protected] to get that specific version, e.g. [email protected]Owenism
Something similar works for nvm-windows too, e.g. cd ~/AppData/Roaming/nvm/v5.10.1.Defalcation
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 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.Hominy
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.1Chrysoprase
!! Is there a way to switch npm versions with nvm without installing a new/different npm version each time you want to switch?Doff
I am working on this problem right now, any help appreciated - #50976638Doff
Even easier: nvm install-latest-npm as answered below by samlandfried -https://mcmap.net/q/73567/-how-can-i-change-the-version-of-npm-using-nvmCorybantic
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.Eucharist
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.Julissa
This doesn't work for nvm-windows, check this - #54652881Rill
This didn't change due to the passage of time alone. Can you add version information to the answer? What version did it change in? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)Flanna
"Newest" and "now" get dated very quickly. This answer shouldn't read as a revision history. It should be written as if it was written today. Either provide the exact version ranges (along with release dates) for which certain parts are valid. For example, with sub sections headings "Version 4.35.2 (2020-09-18) or later", "Version 2.10.9 - 4.35.1 (2015-01-29 - 2020-09-18)", and "Version 2.10.8 (2015-07-02) or earlier" . Or only provide it for some new version if older versions are not that interesting.Flanna
Does this work for anybody now with nvm 1.1.10 on windows? When I run nvm install-latest-npm it just gives me the same as nvm help. When I run nvm install --latest-npm it errors.Sinner
I cannot get this to work with nvm 1.1.10 or 1.1.11Karame
L
185

Use

npm install [email protected] -g
npm install [email protected] -g
Labarum answered 5/9, 2017 at 20:53 Comment(3)
Though the question is about nvm, I actually came here for that, thanks. Upvoting.Disregard
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.Substituent
An explanation would be in order. E.g., what is the idea/gist? What is it supposed to do? Is "x.x.x" literal or not? Is it installing two versions side by side (at the same time)? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).Flanna
M
59

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.

Monometallism answered 10/4, 2012 at 18:40 Comment(5)
Thanks! A note that the url has changed to: curl https://npmjs.org/install.sh | shCislunar
Is this a really old answer now? My npm is downloaded by nvm automatically as a dependency of the node version I'm using.Farley
Yeah. It's old. If you're still using node 0.4 and an old nvm you might see this issue.Monometallism
The URL has been changed again to force www. Use curl -L to follow the redirect.Gerrilee
To install a specific npm version: curl -L https://www.npmjs.com/install.sh | npm_install="6.14.18" shHolub
P
49

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

Phosgene answered 27/11, 2017 at 20:19 Comment(7)
Do you have a link to the documentation for this?Conduction
From nvm help console output: --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version I didn't find a reference to this in the nvm docs, though. Please share if you find it.Phosgene
Thank you! This really should be marked as the correct answer.Corybantic
its only available on 32 bitsNijinsky
when i ran this, it updated node as wellWhitten
Is it possible to save the npm version into an .nvmrc-like file?Heida
Maybe obvious, but this works until the latest includes a breaking change, e.g., upgrading npm v6 (from node 14) to v8.1.0 when you really wanted v7Emelineemelita
C
49

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.

Cigar answered 20/6, 2018 at 19:0 Comment(8)
Thanks, this worked for me as well, on Windows. (had same issue of file conflicts)Bryology
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.Whitening
Thank you, they probably need to fix the way npm update on every old nodejs version on nvm windows.Trinee
That helped me to update npm which came with Node 10.x. Thank you!Zoila
Worked on windows with nodejs v10.15.3, had to change mv to ren and rm to del and rmdir for me.Malley
I found that when upgrading from npm@6, I also had to rename npx and npx.cmd. Not sure what version introduced npx.Silverts
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) '''Jose
Doesn't work for me with node 14.18.0: I get errors like npm ERR! Error: EPERM: operation not permitted, rename 'C:\Users\roryk\AppData\Roaming\nvm\v14.18.0\node_modules\npm\node_modules\.node-gyp.DELETE\node_modules\@npmcli' -> 'C:\Users\roryk\AppData\Roaming\nvm\v14.18.0\node_modules\npm\node_modules\node-gyp\node_modules\@npmcli'Sinner
C
41

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]
Cw answered 12/1, 2018 at 6:1 Comment(1)
Yes, I ended up just doing this on Windows. nvm-windows doesn't seem to have install-latest-npm command, and this seemed to do the trick just fine.Submicroscopic
C
21

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
Cowans answered 13/11, 2015 at 1:58 Comment(3)
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.Melloney
After trying several things, for me, just using sudo npm install -g [email protected] gets me that version responding from npm -v. This at least worked going from 4.x to 2.15.9, anyways.Melloney
In my case sudo npm install -g [email protected] only installed 4.6.1 but npm 5.1.0 was still used. Your tip help me switch to 4.6.1.Allare
E
15

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)
Encephalic answered 20/5, 2022 at 6:38 Comment(0)
H
11
  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.

Haslam answered 7/8, 2019 at 4:47 Comment(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.Informant
S
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 version they have marked as latest.

Seamy answered 7/12, 2015 at 10:54 Comment(1)
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.Melloney
O
8

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
Oversubtle answered 11/8, 2021 at 15:39 Comment(1)
npm and n both manages nodejs versions not npm versions.Gaul
P
4

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

Phantasmagoria answered 18/12, 2018 at 19:58 Comment(0)
K
3

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

Kenna answered 30/10, 2017 at 16:32 Comment(1)
This is always the quickest and easiest I normally use.Anglian
B
3

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
Bename answered 11/11, 2020 at 8:20 Comment(0)
Q
2

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
Quintillion answered 28/12, 2017 at 5:16 Comment(0)
N
2

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.

Nonbeliever answered 17/5, 2022 at 10:22 Comment(0)
C
1

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.

Coquina answered 23/12, 2022 at 23:26 Comment(0)
N
0

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).

Nyberg answered 19/3, 2021 at 19:48 Comment(0)
D
0

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.

Damask answered 23/8, 2022 at 11:7 Comment(1)
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.Unqualified
C
0

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
...
Conti answered 16/2, 2023 at 22:4 Comment(0)
P
0

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

Pokeweed answered 20/9, 2023 at 12:6 Comment(0)
S
-1

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.
Salahi answered 24/6, 2019 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.