[node.js] npm ERR! Error: EPERM: operation not permitted, rename

When I execute npm install I get this error

npm ERR! Error: EPERM: operation not permitted, rename C:\projects******\node_modules\react-async-script' -> 'C:\projects*******\node_modules.react-async-script.DELETE'

  • I am running the cmd as administrator
  • I ran npm cache clean before
  • I made sure all the other applications don't have anything related to node_modules open

This question is related to node.js windows npm

The answer is


I was getting that same error and according to https://github.com/Medium/phantomjs/issues/19 it could be caused by your antivirus software. I disabled mine for the duration of the install and executed "npm install" on cmd as admin and it worked. Hope this helps.


Closing PHPStorm fixed the issue for me.


In my case, I just run npm install with cmd as admin


If you want to avoid the --force option (which is always a better approach), I suggest making sure that you have stopped running the project, as this is usually the main reason for locking the files in almost 90% of the cases I have seen

I suggest the following steps in this order:

1- In Angular stopping ng s and in React stopping npm start usually solves this issue because usually this error happens if a development server is running the project as it locks some files & then npm can't update them thus throwing this error

2- If the above doesn't work, then try closing the code editor that has the workspace opened in it (maybe it was locking some files or something)

So try closing the code editor & running:

npm install

3- If still it doesn't work, then maybe you can try the --force option

npm install --force

npm was failing for me at scandir for:

npm install -g webpack

...which might be caused by npm attempting to "modify" files that were potentially locked by other processes as mentioned here and in few other github threads. After force cleaning the cache, verifying cache, running as admin, disabling the AV, etc the solution that actually worked for me was closing any thing that might be placing a lock the files (i.e. restarting my computer).

I hope this helps someone struggling.


In my situation this helped:

Before proceeding to execute these commands close all VS Code instances.

  1. clean cache with

     npm cache clean --force
    
  2. install the latest version of npm globally as admin:

     npm install -g npm@latest --force
    
  3. clean cache with

     npm cache clean --force
    
  4. Try to install your component once again.

I hope this works for others, if not you may also try temporarily disabling antivirus software before trying again.


This is a typical error caused by Antivirus. There is a workaround for cases like mine, where I can't disable A/V (Company Policy).

You have to change the polyfills.js inside Npm package:

[NODE_HOME]/node_modules/npm/node_modules/graceful_fs/polyfills.js

Look for this statement:

if (process.platform === "win32") {

Inside of this statement, there is a timeout making a retry in case of error. The problem is that in some cases, after the timeout, the file is still locked by the A/V. The solution is rip out the timeout and let this statement in loop. The change with the previous code commented:

if (platform === "win32") {

fs.rename = (function (fs$rename) { return function (from, to, cb) {
  var start = Date.now()
  var backoff = 0;
  fs$rename(from, to, function CB (er) {
    if (er
        && (er.code === "EACCES" || er.code === "EPERM")
        /*&& Date.now() - start < 60000*/) {
            console.log("Retrying rename file: " + from + " <> " + to)
            fs$rename(from, to, CB);
      /*setTimeout(function() {
        fs.stat(to, function (stater, st) {
          if (stater && stater.code === "ENOENT")
            fs$rename(from, to, CB);
          else
            cb(er)
        })
      }, backoff)*/
      if (backoff < 100)
        backoff += 10;
      return;
    }
    if (cb) cb(er)
  })
}})(fs.rename)
}

I got it working when tried npm install with a force option to fetch remote resources even if a local copy exists on disk. Try running

npm install --force

Updated For some of my colleagues this solution was not working. But we tried using yarn instead of npm. It works (faster as well) without any issues all the time

yarn install [package-name]

I had the same problem after updating to npm to 5.4.2, npm start giving the same error for most npm commands. Some solution suggest to run it with --no-optional, but it didn't always work.

Others suggested to downgrade, but I didn't want to downgrade.

I suspected that there was a problem with the installation, not sure what it was.

So I re-updated my npm:

npm i -g npm

and worked fine since then.


just close the VS Code to solve the issue for me


I did the following:

  1. Upgraded the npm to the latest version: npm install -g [email protected]
  2. deleted the npm-cache folder from: c:/users/[username]/AppData/Roaming/npm-cache
  3. Did cache clean : npm cache clean --force
  4. Ran the npm install.

My answer is to do npm cache clean --force first. Then run npm install -g [email protected] to install npm 5.3.0 version. for me, this npm version works with no trouble.


Open the command prompt as administrator and navigate to the project location and then run npm install. it worked for me.


In my case, something got locked up and a quick reboot resolved it.


I closed VS, deleted the node_modules folder.

Then ran:

npm i -D -E [email protected] css- 
[email protected] [email protected] mini-css- 
[email protected] [email protected] [email protected] 
[email protected] [email protected] 

Then had to change the property to not read only on node_modules folder once it got done running.

Then ran:

npm i @microsoft/signalr @types/node

Then opened back up the project in VS and the package.json looked right with the dependencies.

Along the same lines as others talking about read only on node_modules folder and closing down VS to run npm install over.


I have had this issue multiple times only in Windows I try these in the order usually.

  1. npm install --force
  2. Check if node_modules is set to read-only and remove if it is
  3. Delete node_modules/
  4. Check if any editor is opened that could have access to the root folder of the project
  5. Reboot :(

Usually trying npm install after one of those steps will resolve it.


In my case running npm: v6.9.0 && node: v10.16.1 on Windows, I had to run my CLI (git bash) as administrator. Then packages were installed without any warnings/errors


After a lot of try I resolved this with following steps:

 0. Run `cmd` or `powershell` as admin
  1. clean cachec npm clean cache --force
  2. delete insides of %AppData%/npm-cache/
  3. delete %temp%
  4. Delete the npm module which is causing the error. For me it was iconv. So deleted iconv and iconv-lite
  5. When I built again, I got failure reason as a virus inside app-builder. I ran windows defender on node-module and removed that virus. Then, build again.
  6. This time I was successful.

Running commands as Super Admin worked for me. Retry after closing Editor your are working in.


Most likely the node_modules folder became Read Only. You can try updating folder permissions but if you do not have admin access, the npm install --force will work.


I am using macOS catalina ,

      npm init 

I got error

       operation not permitted, uv_cwd

in 2021, this is how you can fix this problem.

very simple:

step 1: go to parent folder

         cd ../
         

step 2: go to your project folder again,

         cd  your-project-folder

That is it. it works.


For me i just closed the Code editor (VS Code) and then run the same command. And that solves the issue for me.


These steps solved for me

Go to the package.json file in the file explorer Right click & select Properties. Deselect Read-only. Click Apply

NOTE: (In case if it is already deselected , check and uncheck the read-only once and click Apply)


I'm using the terminal in VSCode and I realized I was using the bash terminal instead of the node terminal.


I face this issue multiple times. It turns out, that it has nothing to do with permissions, cache, etc. The error message is misleading. For node v 6.x you will see more detailed error stack but not after 7.x For me and my colleges, the issue is timeout function. Basically the package install has not finished yet (i.e. holding the directory) when npm tries to delete it. in node 6.x you can see that in finalize.js now it's gone! Just use yarn.


If you are having the issue on PHP Storm:

  1. Close terminal tab
  2. Open a new terminal tab

I struggeled with this too. I finaly a solution that works fine if you use nvm:

cd ~/.nvm/versions/node/{your node version}/lib/ npm install npm

and that's it.


Trying to rename a file to another filename that already exists can cause an EPERM error on Windows.


I was getting the same thing. I didn't find this anywhere but it hit me that our VMs tend to change files to a read-only state. So I opened package.json's file properties and deselected Read-only in the "General" tab.

Steps

  1. Go to the package.json file.
  2. Right-click and select Properties.
  3. In the General tab, Attributes section, deselect Read-only.
  4. Click Apply to apply the change.

I had the same problem. The reason for the error is unsupported characters in the path to the file. Replaced the cyrillic in English - it helped.


run the CLI as administrator always solve the problem for me


Just delete the package folder from the path of npm global packages. then reinstall the package. Note that the packages are globally installed in: C:\Users\your-name\AppData\Roaming\npm\node_modules


I was having the same problem in windows 10. Just follows these steps :

  • Open cmd as administrator.

  • install uppdated npm package.

  • npm i -g npm

  • npm init --yes


This might be due to your Antivirus software. If you can not disable AV then you can try modifying your NPM global install location as node installs into APPDATA directory which is actively monitored by AV Engines. Try running following commands-

npm config set prefix "YOUR CUSTOM LOCATION" npm config set cache "YOUR CUSTOM LOCATION"

Delete node_modules directory and install your package again.


For some, AppRoot\npm folder could be an issue. Sometimes the AppRoot folder is marked as network shared and thus Antivirus blocks the stuff. Follow below link for complete solution.

https://alastaircrabtree.com/fixing-intermittant-eperm-operation-not-permitted-on-npm-install/

I hope this helps.


I remounted my window disks with the metadata flag, and instantly helped: https://devblogs.microsoft.com/commandline/chmod-chown-wsl-improvements/

after that no need anymore to use sudo for npm commands as the metadata keeps windows and linux file/directory permissions in check.


I'm using iTerm. After restarting iTerm, the error was gone.


Not package.json, but for whatever reason, my node_modules/ had become read-only. Resetting that fixed this.


As Martin Kearn pointed out in a comment, closing Visual Studio resolved these npm permission issues for me. Evidently VS sometimes locks files in node_modules.


In my case setting typescript.disableAutomaticTypeAcquisition in Visual Studio Code to true seemed to help.


Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?