58

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
  • 4,913
  • 3
  • 22
  • 21
Peter T.
  • 2,633
  • 5
  • 27
  • 40

10 Answers10

66

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\v8.10.0           # or whatever version you're using
move npm npm-old
move npm.cmd npm-old.cmd
cd node_modules\
move npm npm-old
cd npm-old\bin
node npm-cli.js i -g npm@latest

And boom, upgraded.

Ryan Shillington
  • 19,533
  • 12
  • 88
  • 99
  • 4
    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
  • 1
    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
  • 3
    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
17

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
  • 5,793
  • 5
  • 23
  • 56
Jeff Kilbride
  • 2,134
  • 1
  • 18
  • 20
  • 3
    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
9

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
  • 419
  • 6
  • 9
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
  • 5,793
  • 5
  • 23
  • 56
Georgi Marinov
  • 81
  • 1
  • 4
  • 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
  • 6,857
  • 3
  • 23
  • 37
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
  • 5,793
  • 5
  • 23
  • 56
Clayton Bell
  • 387
  • 2
  • 4
1

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,524
  • 14
  • 15
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
  • 52
  • 2
1

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
  • 699
  • 5
  • 15
  • 1
    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

This worked for me:

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

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

Nimantha
  • 5,793
  • 5
  • 23
  • 56
scr2em
  • 859
  • 8
  • 15