165
votes

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
30
running the cmd as administrator worked for meHomer
In my case I had to run it as normal user (not administrator) and it worked, it appears to be user permissions conflict. But it seems all other solutions given here npm clean, npm install are not required. Its a user permission issue. If more people can confirm, I can post a new answer.MG Developer

30 Answers

227
votes

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 fix your issue. If not, you may temporarily disable your antivirus program and try again.

31
votes

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]
28
votes

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

26
votes

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

24
votes

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
16
votes

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.

10
votes

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.

9
votes

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.
8
votes

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)
}
5
votes

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.

4
votes

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

4
votes

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

3
votes

Closing PHPStorm fixed the issue for me.

2
votes

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.
2
votes

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.

2
votes

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.

2
votes

just close the VS Code to solve the issue for me

1
votes

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.

1
votes

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.

1
votes

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.

1
votes

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

1
votes

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

1
votes

If you are having the issue on PHP Storm:

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

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

1
votes

run the CLI as administrator always solve the problem for me

1
votes

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.

0
votes

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.

0
votes

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

0
votes

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

0
votes

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