coreybutler / nvm-windows

A node.js version management utility for Windows. Ironically written in Go.
MIT License
38.23k stars 3.38k forks source link

Cannot update npm (node 8.4.0) #300

Closed t0lkman closed 1 year ago

t0lkman commented 7 years ago

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

I'm using NVM4W version:

I have already...

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

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

manuelbieh commented 7 years ago

Same problem here.

kuncevic commented 7 years ago

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

alexgorbatchev commented 7 years ago

Seeing same problem

cannap commented 7 years ago

same windows 10 node 8.5.0 npm 5.3.0 nvm 1.1.0

MichalLytek commented 7 years ago

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

wayofthefuture commented 7 years ago

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

wayofthefuture commented 7 years ago

They should rename continuous integration to continuous intebreaktion

wayofthefuture commented 7 years ago

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

coreybutler commented 7 years ago

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.

wayne-werner commented 7 years ago

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 commented 7 years ago

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

MichalLytek commented 7 years ago

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

wayne-werner commented 7 years ago

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

coreybutler commented 7 years ago

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)
wayne-werner commented 7 years ago

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 notifications@github.com 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- .

kuncevic commented 7 years ago

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
+ npm@5.4.2
removed 1 package and updated 2 packages in 12.125s
coreybutler commented 7 years ago

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

wayne-werner commented 7 years ago

npm -g install npm@5.4.2 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 notifications@github.com 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- .

doug2k1 commented 7 years ago

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.

manuelbieh commented 7 years ago

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.
yavorski commented 7 years ago
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
+ npm@5.4.2
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?

coreybutler commented 7 years ago

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

coreybutler commented 7 years ago

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.

yavorski commented 7 years ago

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

larryboymi commented 7 years ago

Having same issue here...

magicds commented 7 years ago

npm version 5.4.2 node version 8.7.0 window 10

I have the same problem。

farnetani commented 7 years ago

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

and after

npm i -g npm@latest

Solved to me!

Mohamed3on commented 7 years ago

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.

thegreatco commented 7 years ago

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 commented 7 years ago

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

kuncevic commented 7 years ago

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.

AleCaste commented 7 years ago

My solution is NOT very elegant but it works. Here it is. After running npm install npm@5.6 -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 npm@5.6 -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 npm@5.6
  7. npm install npm@5.6 -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)
rolf-schmidiger commented 6 years ago

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
wbt commented 6 years ago

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

ambroselittle commented 6 years ago

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

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

Jameskmonger commented 6 years ago

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.

thegreatco commented 6 years ago

Isn't nvmw a completely different tool?

Jameskmonger commented 6 years ago

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

stramel commented 6 years ago

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/
I-keep-trying commented 6 years ago

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.

arfaWong commented 6 years ago

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.

pumano commented 6 years ago

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
ManifoldFR commented 6 years ago

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

rvkmar commented 6 years ago

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

noelleleigh commented 6 years ago

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

Tested on PowerShell Core 6.0

ZeVS777 commented 6 years ago

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

ststeiger commented 6 years ago

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 ?

ayvarot-zz commented 6 years ago

@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

sebtiz13 commented 6 years ago

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

mhoyer commented 6 years ago

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)