67

I'm using nvm-windows version 1.1.7.

I just installed node 11.9.0.

nvm installs npm version 6.5.0 together with this node version. However, there's npm version 6.7.0 available already.

When I now do npm i -g npm I get:

npm ERR! path C:\Program Files\nodejs\npm.cmd
npm ERR! code EEXIST
npm ERR! Refusing to delete C:\Program Files\nodejs\npm.cmd: is outside C:\Program Files\nodejs\node_modules\npm and not a link
npm ERR! File exists: C:\Program Files\nodejs\npm.cmd
npm ERR! Move it away, and try again.

I found no way to avoid this.

Corey
  • 5,133
  • 3
  • 23
  • 24
Peter T.
  • 2,927
  • 5
  • 33
  • 40
  • Maybe Volta is the tool we are looking for? Check https://volta.sh/ – Rinat Jun 03 '21 at 04:52
  • Actually, I also would recommend switching to another node version manager, such as Volta or fnm that both support Windows. Or, if you are using pnpm (I love it :-D), this has `pnpm env` as builtin command. https://www.honeybadger.io/blog/node-environment-managers/ is a good overview from June 2022 – Peter T. May 30 '23 at 06:33

10 Answers10

83

This is a duplicate from my answer here: https://stackoverflow.com/a/50955293/491553

Here is how I upgrade npm when running nvm-windows:

cd %APPDATA%\nvm\v14.20.0           # or whatever node version you're using
move npm npm-old
move npm.cmd npm-old.cmd
move npx npx-old
move npx.cmd npx-old.cmd
cd node_modules\
move npm npm-old
cd npm-old\bin
node npm-cli.js i -g npm@latest --force

And boom, upgraded.

Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
  • 5
    To find your nvm install path you can run get-command nvm from powershell. – Frank Schwieterman May 10 '19 at 17:28
  • 1
    I have used this command to upgrade to a specific version ``node npm-cli.js i -g npm@x.y.z`` – JB Cha Nov 05 '20 at 16:24
  • 4
    I think we have to use --force in the last command. Btw thanks for the answer. – Utkarsh Jun 11 '21 at 11:45
  • 2
    Those `mv` commands above are for powershell, yet you can't use `%appdata%` syntax in PowerShell so use `$ENV:AppData` in its place. Turned out that my `choco install -y nvm` put nvm in `C:\ProgramData\nvm` so I had to use `cd $ENV:ProgramData\nvm\v14.17.1`. And yes, I had to add the force flag `node npm-cli.js i -g npm@latest --force`. – abulka Jun 18 '21 at 23:26
  • 4
    If there's npx in the directory then even that has to be renamed. In my case I've renamed that as well. `mv npx npx-old` `mv npx.cmd npx-old.cm` – hashBender Jun 19 '21 at 12:30
  • If after the update you're experiencing errors with .ps1 files - it's unrelated, and can be fixed by deleting the .ps1 commands: https://github.com/Azure/azure-functions-core-tools/issues/1821 – Oded Ben Dov Jun 23 '21 at 05:14
  • I'm sorry. I was using `mv` from cygwin. The DOS `move` command should work too. I updated the script so it works now in vanilla DOS. – Ryan Shillington Jun 23 '21 at 18:25
  • Adding to what @hashBender said previously, we have to change the names of `npm.ps1` and `npx.ps1` to `-old` as well. – kaushalpranav Jan 07 '22 at 08:01
  • @ryan-shillington if you can add both these files as well to the answer that would be great – kaushalpranav Jan 07 '22 at 08:07
  • It's almost too easy! – Rory Nov 10 '22 at 12:31
  • 3
    If only it were easy though ... using 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'` – Rory Nov 12 '22 at 21:25
  • > npm WARN I sure hope you know what you are doing. Lol, npm throwing shades at me for running code from stackoverflow :( – Poutrathor Mar 17 '23 at 13:48
  • @kaushalpranav I don't see any `*.ps1` files. Where do you see that? – Ryan Shillington Mar 26 '23 at 05:06
22

Several workarounds are available in this Issue on the nvm-windows github repo:

https://github.com/coreybutler/nvm-windows/issues/300

There are examples using DOS, PowerShell, bash, and batch scripts.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeff Kilbride
  • 2,614
  • 1
  • 20
  • 21
  • 5
    Thanks! The batch script from https://gist.github.com/johnmcase/d31b799b9030327091a0e74880e4c530 worked well for me. – Peter T. Feb 18 '19 at 16:09
  • In my version of the batch script I now added the ` --force` option in the call to npm to avoid an updating error I got: `node "!node_path!\node_modules\npm2\bin\npm-cli.js" i npm@!wanted_version! -g --force` – Peter T. Jan 21 '20 at 11:34
  • https://github.com/coreybutler/nvm-windows/issues/300#issuecomment-798776683 this also worked for me. – Bipin Maharjan Nov 04 '21 at 19:45
11

I have windows 10 operating system.

I installed in following way.

cd %APPDATA%\nvm\v8.11.3
move npm 5.6.0
move npm.cmd 5.6.0.cmd
cd node_modules\
move npm 5.6.0
cd 5.6.0\bin
node npm-cli.js i -g npm@latest
KAUSHIK PARMAR
  • 535
  • 6
  • 11
  • For newer versions, you must do the same with `npx` and `npx.cmd`. Using Gitbash on Windows Terminal the last command must be: `node npm-cli.js i -g npm@latest --force`. If you have a failed attempt, make sure to clean up the `nvm\v8.11.3` folder. – Agustín Amenabar Jul 27 '22 at 12:40
  • Thanks, this worked for me and it updated npx (automatically) too. – pipereset Aug 18 '22 at 20:24
  • How does update existing global packages works? `node npm-cli.js update -g --force` not working! – Sunil Kumar Das Jan 08 '23 at 06:38
7

I had to force it :-/

When it came to

node npm-cli.js i -g npm@latest

I'd rather had to use

node npm-cli.js i -g npm@latest --force

probably to overcome a permission error involved in overwriting the "C:\Program Files\nodejs" link.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Georgi Marinov
  • 91
  • 1
  • 3
  • 5
  • 1
    Ditto - had to force it, but it worked. Glad someone else tried this first though – Trevor Feb 06 '21 at 18:14
  • Just FYI that `--force` did not work for me, and instead left npm in a "broken" state where it had deleted the old install but been unable to install the new one. I fixed it by creating a new folder, running `yarn init`, then `yarn add npm`, then I copied the contents of its `npm` folder into the (now basically empty) `...../nvm/XXX/node_modules/npm` folder, then running: `node ((Get-Item "C:\Program Files\nodejs").Target[0] + "\node_modules\npm\bin\npm-cli.js").ToString() i -g npm@latest` – Venryx Feb 07 '22 at 22:14
6
  1. download this updateNpm.bat file
  2. open powershell in that same folder and run this command updateNpm.bat latest
sytolk
  • 7,223
  • 3
  • 25
  • 38
4

I tried the script and other solutions, this by far is the easiest way:

  1. Navigate to the relevant Node folder (cd C:\Users\yourUser\AppData\Roaming\nvm\vxx.xx.x)
  2. rename npm -> npm2
  3. rename npm.cmd -> npm2.cmd
  4. rename npx -> npx2
  5. rename npx.cmd -> npx2.cmd
  6. Run npm2 install -g npm@your-version
  7. the new npm will create npm, npm.cmd, npx, npx.cmd files, so you can remove the previous renamed files
Yarh
  • 895
  • 7
  • 15
  • 2
    For step 1, this Powershell command will launch an Explorer window in the correct folder: `start ((Get-Item "C:\Program Files\nodejs").Target[0])` – Venryx Feb 07 '22 at 22:11
2

I also found it necessary to install windows-nvm to c:\nvm and c:\nodejs to prevent issues with unsupported paths with spaces.

rm C:\nodejs\npm*
rm C:\nodejs\npx*
mv C:\nodejs\node_modules\npm C:\nodejs\node_modules\npm-old
node C:\nodejs\node_modules\npm-old\bin\npm-cli.js i -g npm@next
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Clayton Bell
  • 424
  • 4
  • 4
2

For me I only get the problem when updating npm with npm v6.
So using a newer version of npm via npx to run the upgrade works for me.

For the very newest version
npx npm install -g npm

Or use a specific version
npx npm@7 install -g npm@7
Nathan Smith
  • 1,643
  • 15
  • 16
  • didn't work for me...same error. – Arst Mar 04 '22 at 05:23
  • Tested with _nvmw 1.1.10_ / _node 14.21.2_ / _npm 6.14.17_ and successfully update to _npm 7.24.2_ w/ `npx npm@7 install -g npm@7` and then can update with `npm install` directly. Note that `npx npm@7 install -g npm` return same error. – Pin Gu Dec 23 '22 at 07:59
  • Got it working with a slight tweak: `npx npm@7 install npm@latest`. Other methods didn't seem to overwrite correct files. Had to specify the `@latest` to force it somehow? – Michael Dandini Mar 31 '23 at 17:51
1

I faced this problem today, the way i solved it was installing latest node with nvm then copying the npm files from latest to the version i am on.

nvm install latest
cd AppData/Roaming/nvm/LATEST
xcopy npm.cmd ../LTS && xcopy npm ../LTS && xcopy node_modules/npm ../LTS

I then confirmed it working by trying to compile my code that breaks on latest.

SoloOrchid
  • 71
  • 4
-2

This worked for me:

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

If you already have git bash installed, use it there.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
scr2em
  • 974
  • 8
  • 17