nvm-windows icon indicating copy to clipboard operation
nvm-windows copied to clipboard

Cannot update npm (node 8.4.0)

Open t0lkman opened this issue 8 years ago • 147 comments
trafficstars

If this is a question about how to use NVM4W, please use stackoverflow instead.

If this is an issue regarding antivirus, make sure you search the existing issues first.

My Environment

  • [ ] Windows 7 or below (not truly supported due to EOL - see wiki for details)

  • [ ] Windows 8

  • [ ] Windows 8.1

  • [x] Windows 10

  • [ ] Windows 10 IoT Core

  • [ ] Windows Server 2012

  • [ ] Windows Server 2012 R2

  • [ ] Windows Server 2016

  • [ ] My Windows installation is non-English.

I'm using NVM4W version:

  • [ ] 1.1.6
  • [x] 1.1.5
  • [ ] 1.1.4
  • [ ] 1.1.3
  • [ ] 1.1.2
  • [ ] 1.1.1
  • [ ] Older
  • [ ] OTHER (Please Specify)

I have already...

  • [x] read the README to be aware of npm gotchas & antivirus issues.
  • [ ] reviewed the wiki to make sure my issue hasn't already been resolved.
  • [x] verified I'm using an account with administrative privileges.
  • [x] searched the issues (open and closed) to make sure this isn't a duplicate.
  • [ ] made sure this isn't a question about how to use NVM for Windows, since gitter is used for questions and comments.

My issue is related to (check only those which apply):

  • [ ] settings.txt
  • [ ] proxy support (Have you tried version 1.1.0+?)
  • [ ] 32 or 64 bit support (Have you tried version 1.1.3+?)
  • [ ] Character escaping (Have you tried version 1.1.6+?)
  • [x] A standard shell environment (terminal/powershell)
  • [ ] A non-standard shell environment (Cmder, Hyper, Cygwin, git)

Expected Behavior

npm i -g npm@latest should install latest npm version

Actual Behavior

an error: 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.

Steps to reproduce the problem:

run as an administrator npm i -g npm@latest

t0lkman avatar Sep 07 '17 08:09 t0lkman

Same problem here.

manuelbieh avatar Sep 07 '17 12:09 manuelbieh

I had the same issue, and here is how I got through :

run npm install -g npm and while it is running: remove(backup) files npm.cmd and npm from c:\Program Files\nodejs\ remove(backup) .bin folder from c:\Program Files\nodejs\node_modules\npm\node_modules, then it should succeed If you have Refusing to deleteissues with any other npm module files/folders you have to just remove these and restart update process. To restart copy npm.cmd back to c:\Program Files\nodejs\ . Then once npm install -g npm is running, remove npm.cmd, otherwise you get in to Refusing to delete npm.cmd issue, you might repeat that process until npm install -g npm is happy.

To remove the files you can use this shortcut: mv npm.cmd "c:\Program Files\nodejs\node_modules\npm\node_modules" as suggested by @farnetani

my env: windows 7 x64, node 8.4, nvm 1.1.6

image

kuncevic avatar Sep 10 '17 23:09 kuncevic

Seeing same problem

alexgorbatchev avatar Sep 16 '17 01:09 alexgorbatchev

same windows 10 node 8.5.0 npm 5.3.0 nvm 1.1.0

cannap avatar Sep 17 '17 12:09 cannap

@kuncevic The easier solution is to copy dir node_modules/npm from the nodejs location, remove the npm bin and cmd, and run node npm-cli.js i -g npm@latest inside bin dir in the copied folder.

MichalLytek avatar Sep 22 '17 14:09 MichalLytek

So much for production. Good luck doing in a script.

wayofthefuture avatar Sep 23 '17 17:09 wayofthefuture

They should rename continuous integration to continuous intebreaktion

wayofthefuture avatar Sep 23 '17 17:09 wayofthefuture

@19majkel94 Couldn't get it working with your method... any other ideas which I could script?

wayofthefuture avatar Sep 25 '17 15:09 wayofthefuture

It appears as though npm has changed how it updates itself... without telling anyone. npx was also introduced with only a minor version change (as opposed to a major change).... which is semantically correct, but still hard to follow along.

Apparently there is some file sandboxing happening. I spent the better part of today investigating and attempting a fix, but each fix surfaces another issue. Bear with me, I'll get it fixed as soon as I can. I'm the only one working on this at the moment... and I happen to be in the middle of a different product launch (after 2yrs of work), so a PR would gladly be accepted.

coreybutler avatar Sep 25 '17 22:09 coreybutler

I wish I could help, but I have an observation... I think that the issues are related to the shortcut to "C:/Program Files/nodejs". Perhaps if a junction were used instead of a shortcut? Windows can't tell the difference between a 'real' directory and a junction. So, it would look like node was really and truly installed in the default directory. No more 'operating' in a directory outside of the resolved directory.

https://docs.microsoft.com/en-us/sysinternals/downloads/junction

wayne-werner avatar Sep 26 '17 15:09 wayne-werner

BTW, using a junction should future proof nvm4w against any future changes to npm's behavior.

wayne-werner avatar Sep 26 '17 16:09 wayne-werner

@wayne-oscme Have you read the readme?

The second option is to use a symlink. This concept requires putting the symlink in the system PATH, then updating its target to the node installation directory you want to use. This is a straightforward approach, and seems to be what people recommend.... until they realize just how much of a pain symlinks are on Windows. This is why it hasn't happened before.

In order to create/modify a symlink, you must be running as an admin, and you must get around Windows UAC (that annoying prompt). Luckily, this is a challenge I already solved with some helper scripts in node-windows. As a result, NVM for Windows maintains a single symlink that is put in the system PATH during installation only. Switching to different versions of node is a matter of switching the symlink target. As a result, this utility does not require you to run nvm use x.x.x every time you open a console window. When you do run nvm use x.x.x, the active version of node is automatically updated across all open console windows. It also persists between system reboots, so you only need to use nvm when you want to make a change.

MichalLytek avatar Sep 26 '17 16:09 MichalLytek

@19majkel94 Please read about junctions. The 'symlink' aka window's shortcut has problems re resolution of target directory from processes. While window's explorer and terminal seem to handle them nicely, they aren't handled nicely in other processes. Open notepad, choose open file to get the standard window's file dialog. Navigate to "C:/Program Files/nodejs" and see where you end up.

Being a redirect to the target directory, which as we see is where processes end up, can lead to some interesting issues.

Junctions are the equivalent of hard links in *nix. Windows only supports these to directories, and not to files, but that's all we need here. They are not redirects. They are, for all intents and purposes, the directory, no different than the path created when the directory was first created.

They don't redirect, they are the destination. They can be deleted and created again with each 'use' command. Therefore, they will have the same effects and capabilities as shortcuts, but none of the downfalls that are creating the current problem.

wayne-werner avatar Sep 26 '17 16:09 wayne-werner

This has nothing to do with junctions/hard links/symlinks. @kuncevic provided a screenshot citing npm errors, the key part being Refusing to delete ... which is outside of ..... That is a hard-coded message in npm, preventing further operations. Bottom line: npm is refusing to full update itself.

This didn't seem to be an issue until npm 5.x.x, and I'm still digging through the details to figure out what has changed. I may have to write a workaround (i.e. hack), which I'm never fond of. I'm also looking into utilizing the msi packages, which seems to be the only way to get the attention of the folks at npm.

If you're in dire need of updating npm, you can download it via https://github.com/npm/npm/archive/vX.X.X.zip and extract it into the node_modules directory of your node installation root. You'll need to manually create a symlink in the node installation root to npm.cmd and npx.cmd. The final file structure should look something like:

nvm
- v8.5.0
   - node_modules
       - npm
          - bin
               - npm.cmd
               - npx.cmd
          ...
   - node.exe
   - npm.cmd (symlink to v8.5.0/node_modules/npm/bin/npm.cmd)
   - npx.cmd (symlink to v8.5.0/node_modules/npm/bin/npx.cmd)

coreybutler avatar Sep 27 '17 13:09 coreybutler

Okay, I guess I should try before selling. I replaced the shortcut with a junction, and got the same exact issue. Thought I had something there. Sorry to waste your time.

On Wed, Sep 27, 2017 at 9:38 AM, Corey Butler [email protected] wrote:

This has nothing to do with junctions/hard links/symlinks. @kuncevic https://github.com/kuncevic provided a screenshot citing npm errors, the key part being Refusing to delete ... which is outside of ..... That is a hard-coded message in npm, preventing further operations. Bottom line: npm is refusing to full update itself.

This didn't seem to be an issue until npm 5.x.x, and I'm still digging through the details to figure out what has changed. I may have to write a workaround (i.e. hack), which I'm never fond of. I'm also looking into utilizing the msi packages, which seems to be the only way to get the attention of the folks at npm.

If you're in dire need of updating npm, you can download it via https://github.com/npm/npm/archive/vX.X.X.zip and extract it into the node_modules directory of your node installation root. You'll need to manually create a symlink in the node installation root to npm.cmd and npx.cmd. The final file structure should look something like:

nvm

  • v8.5.0
    • node_modules
      • npm
        • bin
          • npm.cmd
          • npx.cmd ...
    • node.exe
    • npm.cmd (symlink to v8.5.0/node_modules/npm/bin/npm.cmd)
    • npx.cmd (symlink to v8.5.0/node_modules/npm/bin/npx.cmd)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/coreybutler/nvm-windows/issues/300#issuecomment-332523342, or mute the thread https://github.com/notifications/unsubscribe-auth/AEZgRbY_GVys0bfnD_kaM_PYl6QUmZKDks5smk_PgaJpZM4PPcf- .

wayne-werner avatar Sep 28 '17 03:09 wayne-werner

Just updated 5.4.1 → 5.4.2 with no issues.

c:\>npm install npm -g
C:\Program Files\nodejs\npx -> C:\Program Files\nodejs\node_modules\npm\bin\npx-cli.js
C:\Program Files\nodejs\npm -> C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
+ [email protected]
removed 1 package and updated 2 packages in 12.125s

kuncevic avatar Sep 29 '17 11:09 kuncevic

@kuncevic - thanks... that's indicative the problem must be within npm 5.4.3+.

coreybutler avatar Sep 29 '17 15:09 coreybutler

npm -g install [email protected] still fails for me, same errors. [Edit] BTW, that's starting from 5.3.0, which was installed with nvm install 8.5.0

On Fri, Sep 29, 2017 at 11:41 AM, Corey Butler [email protected] wrote:

@kuncevic https://github.com/kuncevic - thanks... that's indicative the problem must be within npm 5.4.3+.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/coreybutler/nvm-windows/issues/300#issuecomment-333161774, or mute the thread https://github.com/notifications/unsubscribe-auth/AEZgRQnw_jxuz_dDZl3OsZVO_MNkHlBGks5snQ-qgaJpZM4PPcf- .

wayne-werner avatar Sep 29 '17 15:09 wayne-werner

For me it fails from Node 8.4.0 and up. It installs NPM 5.3.0, and then won't allow me to upgrade it. Older Node versions (I have 4.8.4) allow NPM to update normally.

doug2k1 avatar Sep 29 '17 16:09 doug2k1

Man. npm 5 and nvm really sucks big time (but I blame npm 5 here since 4 works perfectly fine). I've downgraded to npm 4.6.1 last week and just tried to give 5.4.2 another try. Looks like npm has been uninstalled while trying to install react-native-cli, a completely different package:

Manuel@Manuel-406 /cygdrive/d/htdocs/wallet
$ npm install -g react-native-cli
C:\Program Files\nodejs\npx -> C:\Program Files\nodejs\node_modules\npm\bin\npx-cli.js
npm WARN Error: EPERM: operation not permitted, scandir 'C:\Program Files\nodejs\node_modules\npm\node_modules\libnpx\node_modules\yargs\node_modules\os-locale\node_modules\execa\node_modules\cross-spawn\node_modules\shebang-command\node_modules'
npm WARN  { Error: EPERM: operation not permitted, scandir 'C:\Program Files\nodejs\node_modules\npm\node_modules\libnpx\node_modules\yargs\node_modules\os-locale\node_modules\execa\node_modules\cross-spawn\node_modules\shebang-command\node_modules'
npm WARN   stack: 'Error: EPERM: operation not permitted, scandir \'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\libnpx\\node_modules\\yargs\\node_modules\\os-locale\\node_modules\\execa\\node_modules\\cross-spawn\\node_modules\\shebang-command\\node_modules\'',
npm WARN   errno: -4048,
npm WARN   code: 'EPERM',
npm WARN   syscall: 'scandir',
npm WARN   path: 'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\libnpx\\node_modules\\yargs\\node_modules\\os-locale\\node_modules\\execa\\node_modules\\cross-spawn\\node_modules\\shebang-command\\node_modules' }
npm ERR! path C:\Program Files\nodejs\npm
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall open
npm ERR! Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\npm'
npm ERR!  { Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\npm'
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\npm'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'open',
npm ERR!      path: 'C:\\Program Files\\nodejs\\npm' },
npm ERR!   stack: 'Error: EPERM: operation not permitted, open \'C:\\Program Files\\nodejs\\npm\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'open',
npm ERR!   path: 'C:\\Program Files\\nodejs\\npm',
npm ERR!   parent: 'v8.5.0' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Manuel\AppData\Roaming\npm-cache\_logs\2017-10-02T14_54_04_833Z-debug.log

Manuel@Manuel-406 /cygdrive/d/htdocs/_git/wallet
$ npm install -g react-native-cli
sh: npm: Kommando nicht gefunden.

manuelbieh avatar Oct 02 '17 15:10 manuelbieh

  • ubuntu: 16.04.2 LTS
  • nodejs: 8.6.0
  • npm: 5.3.0
npm i -g npm@latest

/home/user/.npm-global/bin/npm -> /home/user/.npm-global/lib/node_modules/npm/bin/npm-cli.js
/home/user/.npm-global/bin/npx -> /home/user/.npm-global/lib/node_modules/npm/bin/npx-cli.js
+ [email protected]
added 68 packages, removed 164 packages and updated 14 packages in 14.307s

npm -v still prints 5.3.0 and it's not updated?

yavorski avatar Oct 04 '17 12:10 yavorski

@yavorski - this is nvm for Windows. I think you want nvm for *nix.

coreybutler avatar Oct 04 '17 17:10 coreybutler

I have confirmed this issue is sandboxing within npm 5.x.x. Prior editions of npm did not have this. Unfortunately, the fix is non-trivial, and judging by @yavorski's experience, it's not specific to this project.

coreybutler avatar Oct 04 '17 17:10 coreybutler

Yep no nvm in my case. Linux + Nodejs from nodesource.

yavorski avatar Oct 05 '17 11:10 yavorski

Having same issue here...

larryboymi avatar Oct 20 '17 14:10 larryboymi

npm version 5.4.2 node version 8.7.0 window 10

I have the same problem。

magicds avatar Oct 21 '17 15:10 magicds

Windows 10 mv npm.cmd "c:\Program Files\nodejs\node_modules\npm\"

and after

npm i -g npm@latest

Solved to me!

farnetani avatar Oct 27 '17 01:10 farnetani

I have similar behavior with Node v8.9.0. It comes with npm 5.5.1, and when I try to downgrade to npm 4, it gives me the refusing to delete error.

Mohamed3on avatar Oct 31 '17 21:10 Mohamed3on

I tried to manually fix this, by pulling npm from the official zip file, and I got this error: image

I suspect this error is occurring silently and corrupting the install. When I install to C:\nvm, I don't get this error and everything works as expected.

thegreatco avatar Nov 05 '17 21:11 thegreatco

@RichardMisiak did you try moving the nvm install to a path closer to C:\? Like C:\nvm? It cleared up the issue for me.

thegreatco avatar Nov 07 '17 15:11 thegreatco

With the latest node 9.1 that bringing the latest npm 5.5.1 by default there is even more "fun" https://github.com/npm/npm/issues/19019, saying with the next npm release it might be fixed.

kuncevic avatar Nov 13 '17 07:11 kuncevic

My solution is NOT very elegant but it works. Here it is. After running npm install [email protected] -g I got the following error:

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

My workaround was the following:

  1. npm install -g yarn
  2. Restart the computer
  3. Run yarn global bin , and add the folder that is displayed to the PATH env variable of Windows
  4. Go to the folder where npm.cmd is located (check this from the error we just got on the npm install [email protected] -g run: d:\node\ in my case) and: 2.1. Rename npm to npm.original (or just delete it) 2.1. Rename npm.cmd to npm.cmd.original (or just delete it)
  5. Close the terminal and open a new one
  6. yarn global add [email protected]
  7. npm install [email protected] -g (this will install npm using the npm version located on the yarn global bin folder)
  8. yarn global remove npm (to delete the npm version installed by yarn, and keep only the version installed by npm itself)

AleCaste avatar Dec 02 '17 01:12 AleCaste

Just rename the batch BEFORE upgrading: run cmd

cd %ProgramFiles%\nodejs
ren npm.cmd npm2.cmd
ren npm npm2 
npm2 install npm@latest -g

delete the files after upgrade:

del npm2
del npm2.cmd

rolf-schmidiger avatar Dec 29 '17 13:12 rolf-schmidiger

@rolf-schmidiger In my experience I had to do the same renaming with npx and npx.cmd as with npm and npm.cmd.

wbt avatar Jan 08 '18 15:01 wbt

Deliciously simple and effective, @rolf-schmidiger. Thank you.

@coreybutler, any idea on a solution? Just curious. No pressure.

ambroselittle avatar Jan 12 '18 22:01 ambroselittle

None of the solutions listed here worked for me, and in the end it was quicker to use nvmw uninstall to delete the Node installation and then nvmw install to reinstall it.

Jameskmonger avatar Jan 18 '18 15:01 Jameskmonger

Isn't nvmw a completely different tool?

thegreatco avatar Jan 18 '18 15:01 thegreatco

@thegreatco Oops! You're right. Interesting that the same issue occurs on both, though!

Jameskmonger avatar Jan 18 '18 16:01 Jameskmonger

Based on @rolf-schmidiger's answer, I created an update script for it... Kinda sucks that this is the only way I could get it to work.

#!/usr/bin/bash

cd "$PROGRAMFILES"/nodejs
mv npm.cmd npm2.cmd
mv npm npm2
npm2 install -g npm@latest
rm npm2
rm npm2.cmd

UPDATE

This is the latest that is working for me.

#!/usr/bin/bash

cd "$PROGRAMFILES"/nodejs
rm npm npx npm.cmd npx.cmd
mv node_modules/npm node_modules/npm2
node node_modules/npm2/bin/npm-cli.js i -g npm@latest
rm -rf node_modules/npm2/

stramel avatar Jan 25 '18 18:01 stramel

I just installed everything for the first time, so none of the above suggestions seem to apply to me. I keep getting this when trying to execute npx create-react-app:

npx : The term 'npx' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.

Windows 10 Pro, build 1703, fresh re-install about 2 weeks ago with Developer Mode enabled Node version 9.4.0 npm 5.6.0

The install instructions were a little confusing (there are multiple files to download, what is that no-install file for?) but I finally figured out that you have to turn nvm 'on' and then issue the 'use' command, (confused me because I'm not using a separate package manager so I thought that part didn't apply) but then I'm still getting the above error.

I tried every possible thing. I uninstalled nvm and installed a different version, I checked the environment path, I ran out of ideas. I finally just did npm install -g npx and that seems to be working. Screw it.

I-keep-trying avatar Jan 28 '18 23:01 I-keep-trying

I just remove the npm and npm.cmd files from the nodejs location, move dir node_modules/npm from the nodejs location to another location, and run node npm-cli.js i -g npm@latest inside bin dir in the moved folder.

arfaWong avatar Feb 24 '18 02:02 arfaWong

I got same problem (windows 10 environment) when trying to upgrade my node 8.8.0 with npm 5.5.1 to node 9.8.0 but installation of node 9.8.0 does not update my npm from 5.5.1 to latest. After it I try to npm i -g npm and get error:

C:\Repository\myproject>npm i -g npm
npm WARN npm npm does not support Node.js v9.8.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8.
npm WARN npm You can find the latest version at https://nodejs.org/
WARNING: You are likely using a version of node-tar or npm that is incompatible with this version of Node.js.
Please use either the version of npm that is bundled with Node.js, or a version of npm (> 5.5.1 or < 5.4.0) or node-tar (> 4.0.1) that is compatible with Node.js 9 and above.
npm[19860]: src\node_zlib.cc:436: Assertion `args.Length() == 7 && "init(windowBits, level, memLevel, strategy, writeResult, writeCallback," " dictionary)"' failed.
 1: node::DecodeWrite
 2: node::DecodeWrite
 3: uv_loop_size
 4: v8::internal::wasm::SignatureMap::Find
 5: v8::internal::Builtins::CallableFor
 6: v8::internal::Builtins::CallableFor
 7: v8::internal::Builtins::CallableFor
 8: 000003BEBC8843C1

pumano avatar Mar 13 '18 16:03 pumano

@arfaWong 's solution worked perfectly for me (I mixed a bit with @rolf-schmidiger 's solution and renamed instead of moving)

ManifoldFR avatar Mar 15 '18 16:03 ManifoldFR

It is very simple.. If you are on windows kindly check https://github.com/felixrieseberg/npm-windows-upgrade If you want to do it manually kindly check the documentation https://github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows

rvkmar avatar Mar 16 '18 01:03 rvkmar

I threw together a PowerShell script to automate @arfaWong 's solution: https://gist.github.com/noahleigh/ba34e18b3e0bc4a6a4e93ed7a480536e

Tested on PowerShell Core 6.0

noelleleigh avatar Mar 17 '18 19:03 noelleleigh

@rvkmar nope, no luck https://github.com/felixrieseberg/npm-windows-upgrade/issues/124

ZeVS777 avatar Mar 18 '18 18:03 ZeVS777

Same problem with

node-v8.6.0-win-x64
node-v9.9.0-win-x64

and if you rename the file or move them, it will fail with unable to create folder, please re-run as administrator. Appart from the fact that I don't have admin rights on this machine, it is doubtful that this would change anything, as the current user has mkdir permissions in that folder...

When will that ever get fixed ?

ststeiger avatar Mar 28 '18 07:03 ststeiger

@arfaWong rocks!!

I used cmder, nvm 1.1.6 and node 8.10.0 Updated from npm 5.6.0 to 5.8.0 :tada:

cd %programfiles%/nodejs rm npm npm.cmd mv node_modules/npm node_modules/npm2 node node_modules\npm2\bin\npm-cli.js i npm@latest -g rm -rf npm2

ayvarot-zz avatar Mar 28 '18 18:03 ayvarot-zz

thanks for your solution @ayvarot I used nvm 1.1.15 and node 9.8.0 Updated from 5.6.0 to 5.8.0

sebtiz13 avatar Apr 08 '18 21:04 sebtiz13

The solution by @ayvarot worked partially for me. Maybe due to the fact that I was running it on a msys git bash with ConEmu? Anyway, this is my adjusted snippet:

which npm && \
    cd "`which npm | xargs -0 dirname`" && \
    mv npm npm2 && \
    mv npm.cmd npm2.cmd && \
    mv ./node_modules/npm ./node_modules/npm2 && \
    node node_modules/npm2/bin/npm-cli.js i npm@latest -g && \
    rm -r npm2 npm2.cmd ./node_modules/npm2

npm -v

(Using nvm 1.1.6 on node 8.10.0 x32 and updating from npm 5.6.0 to 5.8.0)

mhoyer avatar Apr 16 '18 12:04 mhoyer

@arfaWong That worked... Although not the ideal one... Will wait for npm team to fix it... . Faced this with npm 5.6.0

virtualvishwam avatar Apr 18 '18 14:04 virtualvishwam

A plain Windows DOS version of @ayvarot's

pushd %ProgramFiles%\nodejs
del npm npm.cmd
move node_modules\npm node_modules\npm2
node node_modules\npm2\bin\npm-cli.js i npm@latest -g
rd node_modules\npm2 /S /Q
popd

Jeff-Lewis avatar Apr 21 '18 14:04 Jeff-Lewis

@arfaWong solution worked like a charm, thank you!

lysla avatar Apr 23 '18 08:04 lysla

Windows 10: cd (your nodejs folder) ren npm.cmd npm2.cmd del npm del npx

npm2 install npm -g

del npm2.cmd

I did just rename npm and npx btw, and then delete them afterwards, but I think they could just be deleted straight away.

stgaup avatar Apr 23 '18 09:04 stgaup

This seems now triggered by installation of Nodejs version 10.0.0 using nvm.

BurtHarris avatar Apr 26 '18 17:04 BurtHarris

@arfaWong The only solution that worked guys. Try this out.

nevinm avatar Apr 27 '18 06:04 nevinm

@rolf-schmidiger worked for me! thank you

RickieWoo avatar May 03 '18 03:05 RickieWoo

I got around this by using Yarn, which I already had installed.

nvm use 10.0.0

# Have npm remove itself
npm uninstall -g npm

# Install npm into the yarn directory structure
yarn global add npm

# Use npm to install itself into the nodejs directory structure
npm install -g npm

# Remove the yarn installation of npm
yarn global remove npm

kraihn avatar May 03 '18 20:05 kraihn

This worked for me:

nvm use <some_other_version>
cd .../nvm/<version_to_update>
npm un npm
npm i npm@latest

xmbhasin avatar May 03 '18 20:05 xmbhasin

use yarn instead of npm it self, try yarn global add npm@latest, try to upgrade yarn if this fails.

hwangzhiming avatar May 07 '18 07:05 hwangzhiming

I also got the Refusing to delete error when trying to update npm using npm install npm@latest --global.

The npm-windows-upgrade package was unable to update as well, suggesting to reinstall Node.js, in spite of this being a fresh unzip installation.

I was able to upgrade by moving the npm and npx files, as previously suggested, using this PowerShell script:

# Locate the Node.js installation
# containing the npm and npx files that are preventing the upgrade
Get-Command node.exe | % { Split-Path $_.Source } | cd

$CurrentNpmVersion = npm --version

# Version the current npm scripts to make room for the new version:
"npm", "npm.cmd", "npx", "npx.cmd" | %{ Get-ChildItem -Filter $_ } |
  Rename-Item -NewName { "$($_.BaseName)-$CurrentNpmVersion$($_.Extension)" }

# Use the current npm command to install the new version:
Invoke-Expression "npm-$($CurrentNpmVersion).cmd install npm@latest --global"

awarberg avatar May 10 '18 12:05 awarberg

Hi @awarberg I tried your script but I get this error. I tried with yarn as well, no luck... stupid Windows! 💢

.\update_node.ps1
npm ERR! path C:\Program Files\nodejs\node_modules\npm\node_modules\worker-farm\node_modules\.bin\errno
npm ERR! code EEXIST
npm ERR! Refusing to delete C:\Program Files\nodejs\node_modules\npm\node_modules\worker-farm\node_modules\.bin\errno: is outside C:\Program Files\nodejs\node_modules\npm\node_modules\worker-farm\node_modules\errno and not a link
npm ERR! File exists: C:\Program Files\nodejs\node_modules\npm\node_modules\worker-farm\node_modules\.bin\errno
npm ERR! Move it away, and try again.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\vitom\AppData\Roaming\npm-cache\_logs\2018-05-11T12_55_55_902Z-debug.log

lamuertepeluda avatar May 11 '18 13:05 lamuertepeluda

@lamuertepeluda sorry to hear you still can't upgrade. The file mentioned ...\node_modules\.bin\errno was not mentioned previously. This could be a corrupted installation due to multiple upgrade attempts. May be a good time to reinstall it, as npm-windows-upgrade suggests.

As for "stupid Windows" I don't agree. More likely, NPM was designed for Linux first, and Windows support was bolted on later, as an after thought. I see this regularly in other NPM packages as well. Typical problems are difficulty to install and requires manual and undocumented steps to ensure software prerequisites. Another common problem is case sensitive file system pathing.

awarberg avatar May 11 '18 18:05 awarberg

I made a batch script to auto run @arfaWong's solution if anyone's interested.

@echo off
SETLOCAL EnableDelayedExpansion

if [%1] == [] (
	echo Pass in the version you would like to install, or "latest" to install the latest npm version.
) else (
	set wanted_version=%1

	if "!wanted_version!" == "latest" (
		for /f %%i in ('npm show npm version') do set wanted_version=%%i
	)

	for /f %%i in ('npm -g -v') do set cur_version=%%i

	if "!cur_version!" == "!wanted_version!" (
		echo Already on npm version !wanted_version!.
	) else (
		echo Updating to !wanted_version!...

		set node_path=!PROGRAMFILES!\nodejs

		rename "!node_path!\npm" npm2
		rename "!node_path!\npm.cmd" npm2.cmd
		rename "!node_path!\node_modules\npm" npm2
		node "!node_path!\node_modules\npm2\bin\npm-cli.js" i npm@!wanted_version! -g

		for /f %%i in ('npm -g -v') do set new_version=%%i

		echo New version installed is !new_version!

		if "!new_version!" == "!wanted_version!" (
			echo Successfully updated to !wanted_version!. Cleaning up backups...
			del "!node_path!\npm2"
			del "!node_path!\npm2.cmd"
			@RD /S /Q "!node_path!\node_modules\npm2"
			echo Update complete.
		) else (
			echo Something went wrong. Rolling back.
			if exist "!node_path!\npm" (
				del "!node_path!\npm"
			)
			if exist "!node_path!\npm.cmd" (
				del "!node_path!\npm.cmd"
			)
			if exist "!node_path!\node_modules\npm" (
				@RD /S /Q "!node_path!\node_modules\npm"
			)
			rename "!node_path!\npm2" npm
			rename "!node_path!\npm2.cmd" npm.cmd
			rename "!node_path!\node_modules\npm2" npm
		)
	)
)

kufii avatar May 18 '18 18:05 kufii

I created a gist from @kufii's bat script. Slightly modified so that you can specify the version to install instead of always installing lastet.

https://gist.github.com/johnmcase/d31b799b9030327091a0e74880e4c530

--Edit: Updated to reflect @kufii's updated script above

johnmcase avatar May 18 '18 20:05 johnmcase

@johnmcase good idea, I updated my comment so you can pass in the version you want to install as an argument (or "latest" to get the latest version)

kufii avatar May 18 '18 23:05 kufii

@kufii's updated version works better for me than having the desired number hardcoded into the script.

BurtHarris avatar May 19 '18 18:05 BurtHarris

@kufii @arfaWong

Thank you!! notworthy

SOSANA avatar May 20 '18 18:05 SOSANA

@johnmcase Worked liked a charm, bookmarking.

kamranayub avatar Jun 05 '18 02:06 kamranayub

Only one issue with this script found: when I selected to install 5.6 it successfully installed 5.6.0 and been rolling back as "5.6" differs from "5.6.0"... Thank You for the script though!

mkasprz avatar Aug 01 '18 03:08 mkasprz

Had the same issue again recently with nvm version 1.1.7 The solution of @arfaWong and @ayvarot worked the last time. But this time Idkw the npm folder wasn´t found. So I recommend not to delete the npm2 folder just in case.

mazyvan avatar Aug 08 '18 17:08 mazyvan

I just spent nearly two days debugging errors caused by this problem and tried nearly every fix to upgrade my NPM from 5.6.0 to 6.1.0. @arfaWong's fix worked for me without any further issues. Thank you!

MendelBak avatar Aug 09 '18 15:08 MendelBak

@arfaWong Works!!

apillai-incomm avatar Aug 27 '18 16:08 apillai-incomm

I went about it the yarn method, but this should also work:

  1. Grab npm from https://github.com/npm/cli/releases
  2. Run the following from the directory you extracted it to from its bin folder
    • node npm-cli.js i -g npm@latest
    • It may complain about files needing to be deleted, for me I had my nvm and nodejs in a root directory on my drive so the standard program file location is inapplicable to me. I nuked the npm/npx/node_modules from my nodejs folder (I'm not afraid to redownload packages and I'm working with a fresh install now anyways).
  3. It should install properly and you should be able to delete the folder you extracted and profit!

Fairly similar to this stackoverflow I wrote up. https://stackoverflow.com/questions/49748307/how-to-reinstall-npm

ctsstc avatar Sep 02 '18 21:09 ctsstc

@ayvarot Thanks very much!!!!!!!! Your advice work for me on Windows 10.

DmitriyWebDev avatar Sep 06 '18 17:09 DmitriyWebDev

Worked for me with just renaming %Node%\npm.cmd, %Node%\npm and %Node%\npx.cmd, no directory renaming was required.

Fr0sT-Brutal avatar Sep 25 '18 14:09 Fr0sT-Brutal

@peternann's suggested solution here works, too. https://github.com/coreybutler/nvm-windows/issues/389

For some reason the script above wasn't working for me and I was simply too tired resolving this to find out why.

stephenasamoah avatar Oct 27 '18 15:10 stephenasamoah

This ridiculous bug still persists so I made a script that does the job. Must be placed inside Node folder where npm.cmd live.

:: Update NPM with workaround of "npm ERR! code EEXIST" bug https://github.com/coreybutler/nvm-windows/issues/300

@ECHO OFF

SETLOCAL

SET CDir=%~dp0%

REN "%CDir%\npm.cmd" "npm1.cmd"
DEL "%CDir%\npm"
DEL "%CDir%\npx.cmd"
DEL "%CDir%\npx"

CALL "%CDir%\npm1.cmd" update && DEL "%CDir%\npm1.cmd"

Save this as npmup.cmd and use instead of npm update

Fr0sT-Brutal avatar Dec 11 '18 12:12 Fr0sT-Brutal

when using the script of @kufii Notice that the installation location should be replaced if you changed the path of node.js like me ,just replace the code set node_path=!PROGRAMFILES!\nodejs to set node_path=YOUR_NODE_PATH

hangvane avatar Dec 15 '18 04:12 hangvane

@kraihn solution does not work now with the latest yarn unless you add yarn bin in to the path:

  1. get the bin path by yarn global bin
  2. stick the yarn bin path in to your path variable

kuncevic avatar Dec 18 '18 11:12 kuncevic

Once a year I get excited, decide to install NVM on Windows thinking "this time it will work", but there's always something broken... Oh well, I guess it's back to the Node.js classic installer. I would use WSL, but unfortunately some Electron apps require you to have Node installed on the host OS.

Is this really unfixable by NVM alone? As far as I've read in different threads, it would also be an npm or Node issue, but isn't there anything NVM alone could do about it? Otherwise, this issue kind of renders NVM useless on Windows. Having to delete files ourselves while in the middle of the update process or even create scripts for that or whatever workarounds some people mentioned, is not a solution. It's hell! The only thing I've tried (a comment in another thread, not this one, mentioning a few steps on how to use yarn to add npm) didn't work.

NgxDev avatar Feb 11 '19 17:02 NgxDev

@MrCroft while I agree that it would be nice to have it "just work", things are far from "hell" and even farther from being "useless". I've been a happy user of this project for something like 2 years now. This problem only occurs when you want to update the global npm itself. Installing new node versions via nvm has no problem and new nodes come with newer npm packages. If you find yourself upgrading npm so often in your dev env that your life becomes hell because of this then you're probably doing something wrong. I think only twice since I became a nvm user I had to actually upgrade npm itself and hit this problem. Not as bad as you make it sound.

terebentina avatar Feb 11 '19 18:02 terebentina

Here's my script for upgrading npm on nvm:

https://stackoverflow.com/a/50955293/491553

I just did it on a new install. Works great.

rsshilli avatar Mar 10 '19 20:03 rsshilli

Maybe somebody should test the npm updater WITHOUT ADMIN RIGHTS...

ststeiger avatar Mar 15 '19 10:03 ststeiger

I think this is more of a previous npm version bug than nvm one.

However, to update npm delete in nvm/[node version] folder (for me it is v10.15.3) following files:

C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npm
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npm.cmd
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npx
C:\Users\[user name]\AppData\Roaming\nvm\[node version]\npx.cmd

Rename npm folder to npm2

C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2

Open any console with admin rights (or even right mouse click on vscode shortcut, than right mouse click on Visual Studio Code and hit Run as administrator) and type commands:

cd C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2\bin
node npm-cli.js i -g npm@latest

Update: as @Fr0sT-Brutal pointed out, admin rights shouldn't be needed at this point, because we've just deleted files that created conflicts 👍

Done.

Delete C:\Users\[user name]\AppData\Roaming\nvm\[node version]\node_modules\npm2 folder.

(This is just step by step reproduce what @19majkel94 said above)

createdbyjurand avatar Mar 20 '19 14:03 createdbyjurand

@createdbyjurand: WITHOUT admin rights... You don't need admin rights to replace a bunch of file with another bunch of files... (as long as you have write access to the files/directories)

ststeiger avatar Mar 21 '19 06:03 ststeiger

@createdbyjurand: WITHOUT admin rights... You don't need admin rights to replace a bunch of file with another bunch of files... (as long as you have write access to the files/directories)

I know. But, Program Files folder on Windows 10 has specific rights access. That is why lots of apps install itself lately in Roaming or Local folders. Even Visual Studio Code has two separate installs. One for Local and one for Program Files. One way to workaround this is to change in nvm nodejs folder location. Second, change Folder Security options but that makes this folder unsecure. What I wrote, what @19majkel94 wrote, is a solution to fix npm update just the way it is, using standard nvm install with default settings for people who just want to fix the problem without getting into more and more system or configuration changes. Because even if you change folder or access rights, you still have to do all the steps I listed, and the only change will be using the console without admin rights.

createdbyjurand avatar Mar 21 '19 06:03 createdbyjurand

@createdbyjurand: Just saying, we run node in a corporate environment, and as developers, we have no admin rights. If an npm-update or a script therein requires admin rights, that breaks our dev-environment, and then we can't install any node packages, and then we can't work.

ststeiger avatar Mar 21 '19 09:03 ststeiger

@createdbyjurand what do you need admin rights for?

Fr0sT-Brutal avatar Mar 21 '19 10:03 Fr0sT-Brutal

@createdbyjurand what do you need admin rights for?

Good point, I don't, because I've just deleted files that were creating conflicts 👍

createdbyjurand avatar Mar 22 '19 14:03 createdbyjurand

Oh I see. I never imagined having NPM installed in PF, for me is rather weird. I'd say those who do it are the reasons of their troubles :).

Fr0sT-Brutal avatar Mar 22 '19 14:03 Fr0sT-Brutal

Oh I see. I never imagined having NPM installed in PF, for me is rather weird. I'd say those who do it are the reasons of their troubles :).

Recently yes. Microsoft changed access rights for Program Files folder. That is why a lot of software is moving now towards Roaming or Local folder install directory. And this is why not so long ago everything was working fine with npm, and now suddenly we get this:

$ npm i -g npm@latest
C:\Program Files\nodejs\npx -> C:\Program Files\nodejs\node_modules\npm\bin\npx-cli.js
npm ERR! path C:\Program Files\nodejs\npm
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall open
npm ERR! Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\npm'
npm ERR!  { [Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\npm']
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\npm'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'open',
npm ERR!      path: 'C:\\Program Files\\nodejs\\npm' },
npm ERR!   stack:
npm ERR!    'Error: EPERM: operation not permitted, open \'C:\\Program Files\\nodejs\\npm\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'open',
npm ERR!   path: 'C:\\Program Files\\nodejs\\npm' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\fehTH4RTH\AppData\Roaming\npm-cache\_logs\2019-03-22T14_28_26_153Z-debug.log

createdbyjurand avatar Mar 22 '19 14:03 createdbyjurand

Oh I see. I never imagined having NPM installed in PF, for me is rather weird. I'd say those who do it are the reasons of their troubles :)

@Fr0sT-Brutal well, for the sake of organization, I do it as well. If I already have a system folder designated for applications (Program Files), whenever I install anything - I install it in the "Program Files" folder. Especially not to pollute the drive's root folder with random folders like "nodejs" or "nvm" and also not to get to long paths, like "c:/Users/My Name/Roaming/ApplicationData/whatever", I just prefer "c:/Program Files/whatever". Nonetheless, although it would have really bugged me to work like that (have nvm|node in a place outside program files), still... I've at least tried and I've installed it in c:/nvm (as for node versions, I've tried both c:/node and as a subfolder of nvm: c:/nvm/node something like that). But the issue persisted, npm still couldn't be updated by running npm i -g npm. Thank God, because I wouldn't want to live with things installed outside of the Program Files folder. Of course I finally gave up and still using 1 version of node at all times, installed using the default node installer for Windows from nodejs.org

NgxDev avatar Mar 22 '19 15:03 NgxDev

@createdbyjurand recently? IIRC, PF became write-restricted for a user since XP. @MrCroft I always wondered how people could live with all the stuff buried inside PF... Space in path means quoting is always required, madness with x64-PF and x32-PF adds one iteration for search, and - the most frustrating - a totally flat software structure. I've >100 apps even on home PC and 50 apps even more at work. I would jump out of a window if I had to search for apps inside PF.

Fr0sT-Brutal avatar Mar 22 '19 15:03 Fr0sT-Brutal

You don't have to put it into C:/Users/My Name/Roaming/Whatever you can use any directory as long as it is user-writeable.

I have all programs on D:\Programs, none of them require admin rights, including nodejs, python, cmake, JetBrains, go, OpenOffice, etc.

Then I put them all into the path environment variables and registry for current_user. Works like a charm. No crappy blackbox-installers required.

If I need to find anything, I use locate32, I don't even browse the folders/Desktop/StartMenu. Locate32 is just way faster. Or I run them via CTRL+R, as you can set the aliases in the registry in HKCU.

The only reason to have something in C:\ProgramFiles is when you want to install a certain application for every user on the machine, which as developer in most cases, you don't want, since having programs in systemwide path/registry might break the programs of other users, or inhibit their ability to use a different version than the version you use. Also, it comes in handy on the server - user1 can use his nodejs, user2 another nodejs, and it doesn't bite itselfs.

By removing admin rights and keeping everything in user, you ensure that one user cannot break the the programs & settings of another user. Also you ensure your applications run without admin rights, which is something that might come in handy at times. And at worst, you can always delete and purge a user account and recrate it later.

That way I can run roughly 1'000 programs, all without a single minute spent installing. Even JetBrains IDE and PostgreSQL work that way. The only programs that I couldn't get to work like this are MS-Office, SQL-Server and Visual-Studio. npm shouldn't be added to this disreputable list, and all because it cannot be bothered to move a few existing files instead of overwriting them.

Anyway, I have my own nodejs-update-script now, which detects the nodes folder from the path environment variable.

ststeiger avatar Mar 22 '19 17:03 ststeiger

I just renamed these files to "1" npm1.cmd, npm1, npx1, npx1.cmd

and ran: "npm1 update -g"

It looks like it worked fine, any thoughts?

chidveer avatar Mar 26 '19 16:03 chidveer

perhaps this can be built into nvm - perhaps by adding an npm version flag along with the node version and architecture flag on install, and an update-nvm function

ErynManela avatar Apr 09 '19 14:04 ErynManela

I just renamed these files to "1" npm1.cmd, npm1, npx1, npx1.cmd

and ran: "npm1 update -g"

It looks like it worked fine, any thoughts?

It worked for me too, but I had to delete the folder \nvm\v10.15.3\node_modules\npm\node_modules.bin because the error pointing to this folder still appeared.

Thank you!

brustulim avatar Apr 12 '19 19:04 brustulim

Hey guys i have made a package for update the local npm version of Program Files\nodejs Checkout npm install -g npm-win-upgrade npm-win-upgrade

Not sure if compatible with NVM at all but it use process.execPath to locate node.exe folder, so it should do.

dolpsdw avatar Apr 20 '19 10:04 dolpsdw

@dolpsdw I get the following error when running npm-win-upgrade:

Npm instaled as Global package: uninstalling...                                                                     
(node:10192) UnhandledPromiseRejectionWarning: Error: Command failed: npm uninstall npm -g                          
npm ERR! path C:\Program Files\nodejs\npx.cmd                                                                       
npm ERR! code EEXIST                                                                                                
npm ERR! Refusing to delete C:\Program Files\nodejs\npx.cmd: is outside C:\Program Files\nodejs\node_modules\npm and
 not a link                                                                                                         
npm ERR! File exists: C:\Program Files\nodejs\npx.cmd                                                               
npm ERR! Move it away, and try again.                                                                               
                                                                                                                    
npm ERR! A complete log of this run can be found in:                                                                
npm ERR!     C:\Users\sjodle\AppData\Roaming\npm-cache\_logs\2019-04-23T17_42_11_305Z-debug.log                     
                                                                                                                    
    at ChildProcess.exithandler (child_process.js:281:12)                                                           
    at emitTwo (events.js:126:13)                                                                                   
    at ChildProcess.emit (events.js:214:7)                                                                          
    at maybeClose (internal/child_process.js:915:16)                                                                
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)                                        
(node:10192) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing
 inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (
rejection id: 1)                                                                                                    
(node:10192) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejec
tions that are not handled will terminate the Node.js process with a non-zero exit code.                            

sodle avatar Apr 23 '19 17:04 sodle