nvm-windows: Cannot update npm (node 8.4.0)
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
-
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
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- Older
- OTHER (Please Specify)
I have already…
- read the README to be aware of npm gotchas & antivirus issues.
- reviewed the wiki to make sure my issue hasn’t already been resolved.
- verified I’m using an account with administrative privileges.
- 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+?)
- 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
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 154
- Comments: 150 (10 by maintainers)
Links to this issue
Commits related to this issue
- Add workaround npm 5.x nvm issue See https://github.com/coreybutler/nvm-windows/issues/300 — committed to fractal-code/meteor-azure-server-init by ramijarrar 6 years ago
- Add workaround for npm 5.x nvm issue See https://github.com/coreybutler/nvm-windows/issues/300 — committed to fractal-code/meteor-azure-server-init by ramijarrar 6 years ago
- COMPASS-3093: Trying workaround for npm upgrade https://github.com/coreybutler/nvm-windows/issues/300#issuecomment-360559722 — committed to mongodb-js/compass by imlucas 6 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) filesnpm.cmd
andnpm
fromc:\Program Files\nodejs\
remove(backup).bin
folder fromc:\Program Files\nodejs\node_modules\npm\node_modules
, then it should succeed If you haveRefusing to delete
issues with any other npm modulefiles/folders
you have to just remove these and restart update process. To restart copynpm.cmd
back toc:\Program Files\nodejs\
. Then oncenpm install -g npm
is running, removenpm.cmd
, otherwise you get in toRefusing to delete npm.cmd
issue, you might repeat that process untilnpm 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 @farnetanimy env:
windows 7 x64, node 8.4, nvm 1.1.6
I just remove the
npm
andnpm.cmd
files from the nodejs location, move dirnode_modules/npm
from the nodejs location to another location, and runnode npm-cli.js i -g npm@latest
insidebin
dir in the moved folder.@kuncevic The easier solution is to copy dir
node_modules/npm
from the nodejs location, remove the npm bin and cmd, and runnode npm-cli.js i -g npm@latest
insidebin
dir in the copied folder.Just rename the batch BEFORE upgrading: run cmd
delete the files after upgrade:
I made a batch script to auto run @arfaWong’s solution if anyone’s interested.
They should rename continuous integration to continuous intebreaktion
@arfaWong rocks!!
I used cmder, nvm 1.1.6 and node 8.10.0 Updated from npm 5.6.0 to 5.8.0 🎉
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
This works. To use:
updateNpm.bat
file./updateNpm.bat latest
A batch script which works fine. https://gist.github.com/nokidding/aafaf90adc80cbce54b676340817bb13
@Markus-Hanisch @rsshilli Thank you for that script.
Taking that as a starting point, I’d like to add the following generalized powershell script, which allows you to upgrade NPM regardless of your current Node version and nvm location:
(the only assumption is that nvm stores its versions alongside its exe file, and names them according to the output of
node --version
)nvm
for Linux has thisinstall-latest-npm
command, maybe it is worth integrating the script above innvm-windows
as well.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
This was my fix: 1.Uninstall npm, node, remove all related directories. 2. Remove nvm bloat if any left 3. Install Volta 4. Everything works out of the box + build in rust 5. ??? 6. Profit
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.
UPDATE
This is the latest that is working for me.
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 isv10.15.3
) following files:Rename npm folder to 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:
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)
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.
Problem was being fixed for me like a year ago by switching on mac 😉
I modified this answer for node 15.10.0 and npm 7.5.3 and it very helpful for me. I added one another step for renaming npx too
I got this work by renaming
npm.cmd
tonpm1.cmd
and then deleted other npm files:npm
,npx.cmd
,npx
. Then I rannpm1 install -g npm
@kuncevic
Corrected: Problem was being avoided by me like a year ago by switching on mac 😉 🤣
So much for production. Good luck doing in a script.
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?
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 thenode_modules
directory of your node installation root. You’ll need to manually create a symlink in the node installation root tonpm.cmd
andnpx.cmd
. The final file structure should look something like:I asked CHATGPT and it suggested to upgrade npm with
npm install -g npm@latest
If you’re using nvm-windows to manage your Node.js versions on Windows and you want to update npm to X version, you can follow these steps:
Open a Command Prompt or PowerShell window.
Make sure you’re using the Node.js version you want to update by running the following command:
Replace
<version>
with the Node.js version you want to use.To update npm, you can use the following command:
This will install the X version of npm globally on your system.
Once the installation is complete, verify the npm version by running:
You should see the X version of npm that you installed.
By following these steps, you should be able to update npm to the X version using nvm-windows on Windows.
Follow this url to know which version of npm belongs to node https://nodejs.org/en/download/releases
Folks, this is an npm issue, not an NVM4W issue. There is a utility for helping with this, available at https://github.com/felixrieseberg/npm-windows-upgrade.
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:My workaround was the following:
npm install -g yarn
yarn global bin
, and add the folder that is displayed to the PATH env variable of Windowsnpm install npm@5.6 -g
run:d:\node\
in my case) and: 2.1. Renamenpm
tonpm.original
(or just delete it) 2.1. Renamenpm.cmd
tonpm.cmd.original
(or just delete it)yarn global add npm@5.6
npm install npm@5.6 -g
(this will install npm using the npm version located on theyarn global bin
folder)yarn global remove npm
(to delete the npm version installed by yarn, and keep only the version installed by npm itself)How come this 5 years old issue is still not fixed…? I believe original nvm has a special command for upgrading npm and it should be the case here too. Having to rely on some obscure .bat scripts found in comments is ridiculous…
I got around this by using Yarn, which I already had installed.
A plain Windows DOS version of @ayvarot’s
For me I only get the problem when updating npm from npm v6. So using a newer version of npm via npx to run the upgrade works for me.
Windows 10
mv npm.cmd "c:\Program Files\nodejs\node_modules\npm\"
and after
npm i -g npm@latest
Solved to me!
@kufii @arfaWong
Thank you!!
Hello @rsshilli thank you for providing the script. It works quite well and updates npm to the latest version as desired. The only minor issue I faced, was that running
npm list -g --depth=0
to list all node packages resulted in two installations of npm. So I simply added some lines of code to remove the “…-old” renamed files and the “…-old” renamed folder. Runningnpm list -g --depth=0
to list all node packages resulted in one single installation of npm as desired. Here is my updated version of your script:cd %APPDATA%\nvm\v8.9.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 install -g npm@latest
cd %APPDATA%\nvm\v8.9.0 # or whatever version you're using
rm npm-old
rm npm-old.cmd
cd node_modules\
rm -rf npm-old
@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.
Yes folks, volta is an option, as is nodist, fnm, nvs, etc. You’re welcome to use those. I’m going to remove comments about other version managers though, because they don’t help this project improve… and that’s the point of the issues for this project.
For anyone finding this issue from Google, please make sure you’ve read the instructions… specifically the ones about removing any prior versions of Node.js (https://github.com/coreybutler/nvm-windows/wiki/Common-Issues#uninstall-existing-node-installation-before-installing-nvm4w). It is possible to update npm, but you cannot have conflicts in your PATH, or permission restrictions… and there are a few older versions of npm with hardcoded pathing issues. When the v1.1.11 patch release drops, use the
debug
function to check your environment for potential problems.I still cannot replicate this problem and there has been a
help wanted
label for 5 years. I believe it when people say they are still running into npm update problems, but without an environment to test on, it’s really hard to troubleshoot. At this point though, with active development proceeding on Runtime and inconsistency in failures, it’s time to close this issue. If anyone runs into this again, please open a new issue and provide details. It’s important to know things like the specific build of Windows, the locale, whether it’s running in an Active Directory controlled environment or not, whether developer mode is on/off, which shell is running the command, whether the shell is run as an administrator or not, whether there are special characters in your install path or not, whether Node was installed without a version manager before or not, whether another version manager was used before NVM4W, etc.This ridiculous bug still persists so I made a script that does the job. Must be placed inside Node folder where
npm.cmd
live.Save this as
npmup.cmd
and use instead ofnpm update
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.
use yarn instead of npm it self, try
yarn global add npm@latest
, try to upgrade yarn if this fails.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:
(Using
nvm
1.1.6 on node 8.10.0 x32 and updating from npm 5.6.0 to 5.8.0)This script works perfect: https://github.com/coreybutler/nvm-windows/issues/300#issuecomment-606682441 https://gist.github.com/nokidding/aafaf90adc80cbce54b676340817bb13
Thank you!
@xmedeko wouldn’t
npx npm-windows-upgrade
run globally unless you’re inside a node project with a local installation ofnpm-windows-upgrade
?Just rename the file
npm1.cmd
, it’s enough. Then run thenpm
update.No need to use
npm-windows-upgrade
.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
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.
@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.
@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)
@arfaWong The only solution that worked guys. Try this out.
I threw together a PowerShell script to automate @arfaWong 's solution: https://gist.github.com/noahleigh/ba34e18b3e0bc4a6a4e93ed7a480536e
Tested on PowerShell Core 6.0
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.same windows 10 node 8.5.0 npm 5.3.0 nvm 1.1.0
Seeing same problem
Usage:
Example:
Thank you
Its been 3 years since this reply, but it still works! Thank you so much
works like charm just be sure ‘set node_path=!PROGRAMFILES!\nodejs’ is pointing correct path in your case specifically incase of no standard installation(zip extract)
@jakobrosenberg Yes, you are right, I didn’t know
npx
behave like that, I always use it for local project packages only.I went about it the yarn method, but this should also work:
bin
foldernode npm-cli.js i -g npm@latest
Fairly similar to this stackoverflow I wrote up. https://stackoverflow.com/questions/49748307/how-to-reinstall-npm
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:
This worked for me:
@rolf-schmidiger In my experience I had to do the same renaming with npx and npx.cmd as with npm and npm.cmd.
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:
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:
And finally, I uninstall nodejs and then re-install it. Then everything is ok! This is what I did:
environment variable path
like:C:\Program Files\nodejs
node -v
andnpm -v
incmd
. If you can see the version, congratulations! No “npm ERR” anymore! If not, add the nodejsenvironment variable path
.That worked for me, at last.
Thank you
Another idea I’ve tried (I don’t know if anyone has already mentioned it) is to install npm directly without using npm itself. Refer to npmjs in the “Direct Download” section, and it works.
Feels kind of mean to offer another package as a solution… but this absolutely worked out of the box for me and I love Rust. Thanks for the info @alekslario
I just needed Step 4, i.e. Just download and fresh install Node JS. Then it worked.
Same Issue! how i solved it:
@kuncevic Suggestion to improve your instructions:
Rather than copy files back and forth or try to move them around quickly enough while the process is running, just invoke npm’s script directly with the node command.
PowerShell:
Command Prompt:
Then you can just remove the files once and restart the process without putting them back if needed.
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.
@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.
@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)
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.
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
toset node_path=YOUR_NODE_PATH
@arfaWong 's solution worked perfectly for me (I mixed a bit with @rolf-schmidiger 's solution and renamed instead of moving)