How to Fix - NPM ERR syscall rename

Mar 26, 2023 | Read time 10 minutes

🔔 Table of contents

Introduction

Sometimes when working on Node or Javascript projects you get random error popping up and stopping your work trying to fix it.

A recent error that I have randomly saw was the npm ERR! syscall rename error.

After running npm install it worked the first time. But then a day or later, I ran npm install and it gave me a error that looks like the following:

npm ERR! code EPERM
npm ERR! syscall rename
npm ERR! path C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\.node-gyp.DELETE\node_modules\@tootallnate
npm ERR! dest C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\node-gyp\node_modules\@tootallnate
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, rename 'C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\.node-gyp.DELETE\node_modules\@tootallnate' -> 'C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\node-gyp\node_modules\@tootallnate'
npm ERR!  { [Error: EPERM: operation not permitted, rename 'C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\.node-gyp.DELETE\node_modules\@tootallnate' -> 'C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\node-gyp\node_modules\@tootallnate']
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, rename 'C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\.node-gyp.DELETE\node_modules\@tootallnate' -> 'C:\Users\<username>\Downloads\ServiceQualityFinal\react-reduction\node_modules\npm\node_modules\node-gyp\node_modules\@tootallnate'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'rename',
npm ERR!      path:
npm ERR!       'C:\\Users\\<username>\\Downloads\\ServiceQualityFinal\\react-reduction\\node_modules\\npm\\node_modules\\.node-gyp.DELETE\\node_modules\\@tootallnate',
npm ERR!      dest:
npm ERR!       'C:\\Users\\<username>\\Downloads\\ServiceQualityFinal\\react-reduction\\node_modules\\npm\\node_modules\\node-gyp\\node_modules\\@tootallnate' },
npm ERR!   stack:
npm ERR!    'Error: EPERM: operation not permitted, rename \'C:\\Users\\<username>\\Downloads\\ServiceQualityFinal\\react-reduction\\node_modules\\npm\\node_modules\\.node-gyp.DELETE\\node_modules\\@tootallnate\' -> \'C:\\Users\\<username>\\Downloads\\ServiceQualityFinal\\react-reduction\\node_modules\\npm\\node_modules\\node-gyp\\node_modules\\@tootallnate\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'rename',
npm ERR!   path:
npm ERR!    'C:\\Users\\<username>\\Downloads\\ServiceQualityFinal\\react-reduction\\node_modules\\npm\\node_modules\\.node-gyp.DELETE\\node_modules\\@tootallnate',
npm ERR!   dest:
npm ERR!    'C:\\Users\\<username>\\Downloads\\ServiceQualityFinal\\react-reduction\\node_modules\\npm\\node_modules\\node-gyp\\node_modules\\@tootallnate',
npm ERR!   parent: '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.

What does syscall rename do?

“syscall rename” means that NPM is doing a system call (syscall) operation to rename a file or directory.

So whenever we do a NPM operation such as npm install the “rename” system call is used for various tasks, such as moving or renaming files and directories during the installation, update, or removal of packages. When npm encounters an issue executing the “rename” operation, it will return an error message that includes “syscall rename.”

Steps to fix syscall rename

This error can be due to a few issues - so we need to go though a checklist of things to do to fix it.

  1. Make sure that you are running as admin mode
  2. Check that file is not locked by another process or Antivirus software
  3. Use a node version manager
  4. Remove cache, node_modules folder and package-lock.json and reinstall
  5. Manually change NPM’s default directory

1. Make sure that you are running as admin mode

Although not generally recommended to run npm in admin mode, if you are stuck with this error its most likely due to permissions.

To rule out if the error is due to permissions or not, we can first try to run in admin mode with elevated privileges and then change the permissions of the project folder accordingly.

  • For Windows,, open the Command Prompt or PowerShell as an administrator (right-click the app icon and select “Run as administrator”). Then run npm install.

  • On macOS or Linux, use the sudo command before the npm command (e.g., sudo npm install).

If running in Admin mode works, then its due to permissions. Just change the folder permissions according. As an example, if you are on Linux, you can change it with chmod like so:

chmod u=rwx,g=rx,o=r node_modules

If it is still not working, then move to the next step.

2. Check that file is not locked by another process or Antivirus software

One other reason why the npm ERR! syscall rename is coming up is due to your node_modules folder or project files being locked out by another program or Antivirus software.

  • Make sure to close any other IDE (Visual Studio, Jetbrains), etc when you are running npm install
  • Close any running Antivirus software
  • Make sure you are not running things like liveserver or any automatic build/watch processes. For example, in Angular you have the ng build --watch - make sure that is turned off!

How to check which processes are locking out node_modules folder

In Windows, you can use the built-in Resource Monitor to check if node_modules is locked by a process.

  1. Press Ctrl + Shift + Esc to open the Task Manager.
  2. Click on the “Performance” tab.
  3. At the bottom, click on “Open Resource Monitor.”
  4. In the Resource Monitor window, go to the “CPU” tab.
  5. In the “Associated Handles” section, you’ll find a search box labeled “Search Handles.”
  6. Enter the folder path (or part of it) that you want to check in the search box and press Enter. For example, if you want to check if the folder C:\example-folder is locked by a process, enter example-folder in the search box.

Linux

In Linux, you can use the lsof command (List Open Files) to check if a folder is locked by a process. If lsof is not installed on your system, you can install it using your package manager (e.g., sudo apt-get install lsof for Debian-based systems or sudo yum install lsof for Red Hat-based systems).

Here’s how to check if a folder is locked by a process using the lsof command:

Open a terminal window. Run the lsof command with the folder path you want to check: bash Copy code lsof /path/to/your/folder Replace /path/to/your/folder with the path of the folder you want to check.

3. Use a node version manager

Generally, if this comes down to a permission issue, it is recommended to use a node version manager.

Lets consider we are trying to install some package globally like so:

npm i -g <your-global-package> --force

You will get the error - if you don’t have permissions:

npm ERR! code EPERM
npm ERR! syscall rename

Now to find the NPM default installer by using the command:

npm config get prefix

This will result in something like this:

C:\Users\Huy\AppData\<username>\npm

Go to the folder and update the permissions accordingly.

Alternatively, it is recommended to use a node version manager such as NVM, since tt will install npm packages in your home folder, so you won’t have to use administrator user to install packages.

To work with NVM and manage your Node versions we can do the following:

  1. For windows, we can go to the binary and install NVM located here:

https://github.com/coreybutler/nvm-windows/tags

For linux distros we can do the following:

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install node 
  1. Uninstall all versions of node with the following command:

nvm uninstall <node version>

  1. Reinstall needed node version:

nvm install <node version>

To get the latest node version we can do nvm install latest

  1. Use node version just installed:

nvm use <node version>

In our case, we can use the command nvm use latest

4. Remove cache, node_modules folder and package-lock.json and reinstall

  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

5. Manually change NPM’s default directory

On the command line, in your home directory, create a directory for global installations:

mkdir ~/.npm-global

Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'

In your preferred text editor, open or create a ~/.profile file and add this line:

export PATH=~/.npm-global/bin:$PATH

On the command line, update your system variables:

source ~/.profile

To test your new configuration, install a package globally without using sudo:

npm install -g jshint

Summary

Getting the error of NPM ERR syscall rename can be caused by a variety of different things. Generally, this error means that when running NPM, it is make a system call (syscall) to rename a file or folder and is failing.

This can be due to there is not sufficient permissions to rename the file or folder, there is a lock on the files from another process or that the file does not exist!

To fix this issue, we can try to run as admin to make sure its not a permission issue, find and turn off any processes that is keeping the lock on the file or folder, use a node version manager.

If those steps do not work, we can try the nuke approach and clear cache, remove node_modules folder, remove package-lock.json and reinstall everything!

👋 About the Author

G'day! I am Huy 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