Fixed - npm error refusing to delete

Troubleshoot and resolve the 'npm ERR! Refusing to delete' error in Node.js

Jun 27, 2023 | Read time 9 minutes

🔔 Table of contents


When you are getting the issue “npm error refusing to delete” - this is mainly due to your permissions or that your NPM/ Node setup is corrupt. To fix this error you will have to run NPM as a admin or reinstall NPM and Node.

Recently I was trying to upgrade my npm packages using the following command:

npm update -g

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

Now after a bit of troubleshooting, I found that to fix this issue, I would consider to do the following:

  1. Run the command as an admin - using sudo
  2. Remove node_modules, package-lock.json and clear NPM cache
  3. If you are using NVM, clear files and reinstall
  4. Remove NPM/ Node from your system and install again
  5. Fix for HomeBrew
  6. Close off any processes

1. Run the command as an admin - using sudo

The first thing to check is that you have permissions to do a NPM update! So if you’re on Linux or macOS, you can run the NPM commands with sudo:

sudo npm update -g

Now note that running npm with sudo is generally not recommended - but you have control over your machine and want a quick way to move forward then go ahead.

The recommended option would be to fix the npm permissions. Refer to the following:

If you are on windows then you can run your command prompt as an administrator to grant it elevated permissions:

  • Search for “Command Prompt” in the Start menu.
  • Right-click on the Command Prompt icon.
  • Click “Run as administrator”.
  • Then run the NPM command - eg npm update -g

2. Remove node_modules, package-lock.json and clear NPM cache

If the above steps did not work for you, we can try the scorched earth approach - removing everything (node_modules and package-lock.json) and reinstall using npm install

Tip: Careful when deleting package-lock.json

Keep in mind deleting package-lock.json has its own challenges - it could work well with 1 level of dependencies, but with dependencies thats more than 2 levels deep it could get messy. package-lock.json is there for stability and consider carefully before deleting this!

Just make sure you have a backup safely tucked away somewhere :)

  1. Firstly, open up the terminal and go to the root of your project directory

Tip: Try clear NPM cache

We can try running npm cache clear --force to clear the NPM cache. If this does not work - proceed to step 2

  1. We need to delete the /node_modules with the following command (you might need to use sudo before each command):

rm -rf node_modules

  1. Delete package-lock.json file using the rm command:

rm -rf package-lock.json

  1. Install the dependencies using the following command:

npm install

3. If you are using NVM, clear files and reinstall

Note: This step applies if you have NVM installed. Ignore this if you have not got NVM on your machine. If you know you have the right permissions, but still getting this error - it could be related to your NVM install.

So to get rid of it we can ddo the following:

  1. Delete these four files:
  1. Then in C:\Users\[userName]\AppData\Roaming\nvm\[nodeVer]\node_modules\, rename the npm directory to npm_old

  2. Now go to the \npm_old\bin directory then install NPM at latest using the command below:

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

4. Remove NPM/ Node from your system and install again

So even after the previous options, you still have this error, then I would go ahead and reinstall NPM and Node again.

Windows option

  • Run npm cache clean –force

  • Uninstall from Programs & Features with the uninstaller.

  • Reboot (or you probably can get away with killing all node-related processes from Task Manager).

  • Check the following folders and remove them. Now the folders may or may not exist since this will depend on the version you installed, UAC settings, and CPU architecture, etc:

C:\Program Files (x86)\Nodejs
C:\Program Files\Nodejs
C:\Users\{User}\AppData\Roaming\npm (or %appdata%\npm)
C:\Users\{User}\AppData\Roaming\npm-cache (or %appdata%\npm-cache)
C:\Users\{User}\.npmrc (and possibly check for that without the . prefix too)
Check your %PATH% environment variable to ensure no references to Nodejs or npm exist.
  • If it’s still not uninstalled, type where node at the command prompt. This will give you the location of Node - make sure to delete that and the parent directory.

  • Restart your machine and install node again.

Hopefully this will fix the “NPM err! Refusing to delete” issue.

Fix for homebrew

This error came up on one of my macOS machines. I was pretty sure that I have the permissions and did all of the remove/reinstall but it still did not work.

I found out that I had HomeBrew on the machine and it was conflicting with NPM. The fix is surprisingly simple. To fix this:

  • Try running brew prune. This neat little command sweeps away those pesky symbolic links cluttering your node locations.

  • After that you can run brew doctor. My problem turned out to be unnecessary post-pruning, but I ran it in my quest for a solution.

It’s a handy tool to keep your system healthy, so no harm done!

Now after you have run the above commands, try

sudo npm install -g npm

Fingers crossed, this should smooth out those npm bumps!

Close processes

One other thing we can try to to make sure that there are no processes hanging on to the NPM files/ folders. So make sure you have closed or kill off any tasks that is using node.

  • Close Your Text Editor/IDE - eg VS Code, Sublime

  • Close Extra Terminal Windows/Tabs: If you have other terminal or command prompt windows/tabs open, close them as they might be running processes holding onto the NPM files/folders.

  • Check for Node Processes:

With Windows, open your command prompt and type tasklist | find "node".

If any node processes appear, you can terminate them with the taskkill command.

For example, if you see a node process with PID 1234, you could kill it with taskkill /F /PID 1234.

On macOS or Linux, open your terminal and type pgrep -l node to find all the node processes.

You can terminate them with the kill command. For example, if you see a node process with PID 1234, you could kill it with kill -9 1234.

  • Restart Your Computer - This is pretty much my go to option when nothing else works.


In this post, I went over the troubleshooting steps for the error “npm ERR! Refusing to delete”. Now this error can be caused by:

  • not having permissions to delete the particular folder. In this case we can use the sudo command
  • Your NPM install is corrupted and need a refresh
  • If you are using a program like homebrew, it could be that it is holding on to the links. In this case, you will need to use the brew prune command.
  • Make sure to kill any processes that are still holding on to the NPM files/folders!

👋 About the Author

G'day! I am Kentaro a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube


Hi, I'm Kentaro - a sofware engineer sharing my programming tips!