247

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
SharpC
  • 6,974
  • 4
  • 45
  • 40
Robin
  • 6,879
  • 7
  • 37
  • 35
  • 6
    running the cmd as administrator worked for me – Homer Feb 04 '19 at 19:19
  • 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 Feb 12 '20 at 17:09
  • in my case, I wasnt using Node.js command prompt. Once I changed it, it works. – a3rxander Jan 02 '22 at 20:45
  • deleted the node_modules folder and it worked – Luv33preet Jun 04 '22 at 09:16
  • I have faced this a number of times on macOS. I have read all the over 30 answers on this question and others. Finally, I took a folder, just as it is and copied it to a drive that does not have any system or admin permission requirements. That would be drive D:\ or E:\ for Windows users. `npm install` - reinstallation for updates happened. `npm run dev` - everything worked as it should. – Afrowave Jun 08 '23 at 05:10

48 Answers48

327

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.

Roman
  • 19,236
  • 15
  • 93
  • 97
  • 70
    Same comment applies to VS Code on Windows … close it and the problem goes away – Martin Kearn Sep 05 '18 at 10:27
  • 6
    For me, I had another node process running, json-server in my case. Once I stopped that the package installed without error. – EddieB Feb 04 '19 at 23:19
  • For me, errors like " ERR! enoent This is related to npm not being able to find a file" got resolved by terminating all the instances for visual studio code. – Abhishek Jul 15 '19 at 10:22
  • Sometimes we oversee the use of webpack which packed all the js files including non-updated module links in eg. app.js. So after npm update, try running `npm run watch`. After that, in my case, the proper module links were updated in a new app.js file. – Peter van der Lely Sep 23 '19 at 15:05
  • @Roman Disabling the anti-virus works for me but what causes the anti-virus to interfere in the Node.js? – Chirag Bhansali Dec 16 '19 at 17:54
  • I have never heard that antivirus plays some role in this process, but everything is possible in this world – Roman Dec 16 '19 at 22:04
  • 58
    `npm WARN using --force I sure hope you know what you are doing.` . . . I have no idea what I'm doing. – Fusseldieb Aug 03 '21 at 18:38
  • 8
    mine was so simple, I closed all vscode projects and restarted it, after that everything started to work fine, but thanks though because after reading this answer and comments I thought of doing that. – faijan memon Nov 29 '21 at 15:05
  • I had this issue on azure when I tried to `SSH` and attempted to `npm install express` in `site/wwwroot` directory and `cache clean --force` fixed my issue. Thanks @Roman – Sangeet Agarwal Feb 26 '22 at 18:03
71

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

Sifat Haque
  • 5,357
  • 1
  • 16
  • 23
  • For me this was caused when I tried to install from VS while ng serve was running. Cancelling ng serve and installing from the command line solved the problem. – Jon Vote Nov 30 '22 at 23:43
44

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

msanford
  • 11,803
  • 11
  • 66
  • 93
Amit Bhosle
  • 601
  • 5
  • 6
  • "Resetting" for me meant `1.` Closing VS, `2.` Deleting `node_modules` from FileExplorer `3.` Restarting my PC `4.` Running `npm install --force`. Then FINALLY it worked. – EGC Feb 06 '20 at 03:58
  • 1
    node_modules folder >> Properties >> uncheck read only worked for me. It happened after copying the folder to the new location. Hope this helps somebody. – brianc Nov 12 '21 at 10:48
  • 1
    Was running 'npm update', EPERM error using Visual Studio 2022 and Git. Had to remove read-only attribute from node_modules folders and children and also from the package-lock.json file. – Timothy Lee Russell Mar 02 '22 at 07:19
  • In my case, as far I can tell `npm` kept resetting the "node_modules" to read-only after each build error. I moved to `yarn`, it is mostly drop in replacement. – user158037 Apr 12 '23 at 09:42
41

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]
sree
  • 2,287
  • 28
  • 26
  • Other people suggested turning off AV software. For me, on the network I am on, this was not an option. The --force flag solved this problem for me – user2590928 Aug 15 '17 at 22:46
  • 4
    I only solved my problem using yarn, so I recommend give a shot to him. – Ângelo Polotto Jun 21 '18 at 14:13
  • Spent lots of time trying to fix the issues with npm (problem global installs not being recognized). Followed your advice of using yarn, did yarn global and now the package I was trying works great! Lateral thinking approach! Thank you! – danivicario Jun 10 '22 at 14:11
33

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
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
  • 4
    This was my problem, I rimraff'd a node_modules folder through the terminal on VS Code - for some reason it didn't quite remove the node_modules from my file system and had it locked. Wasn't until I closed out that window of VS Code and opened it back up that it actually got removed and I could run npm install again. – Stu Furlong Mar 21 '19 at 22:12
17

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.

vmaldosan
  • 444
  • 4
  • 14
  • 1
    Per https://github.com/npm/npm/issues/12059 this has been fixed. No word on what release it will make it into. Per comments, disabling AV does not always work. – user1821052 Nov 04 '16 at 15:05
  • In my case, I had to close SourceTree during the install. – Dejan May 02 '18 at 16:14
11

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.
christo8989
  • 6,442
  • 5
  • 37
  • 43
  • Read Only for me though the original issue was that I usually just use code to install. In this one case I opened a new instance of the app in Visual Studio so I could port some changes from an old version and VS promptly started installing itself. I then opened code to run npm install as I would normally and both apps got into a tizzy. After several fails I then mass dropped a node-modules from a working root version of our app to allow me to back port the changes. This folder was read-only so then carried on blocking things. – Matrim Oct 24 '19 at 13:19
  • 1
    This method works for me. For anyone wondering how to check your permission do: ls -l Then you will see the access permission, in order to change it, a quick way to enable read, write, and Execute permission to user, group, and others, do: chmod 777 fileName – IcyHerrscher Mar 19 '23 at 08:37
9

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)
}
ascripter
  • 5,665
  • 12
  • 45
  • 68
9

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.

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
hoogw
  • 4,982
  • 1
  • 37
  • 33
8

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.

SharpC
  • 6,974
  • 4
  • 45
  • 40
8

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.

jaycer
  • 2,941
  • 2
  • 26
  • 36
5

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

Zach Bloomquist
  • 5,309
  • 29
  • 44
5

just close the VS Code to solve the issue for me

benten
  • 696
  • 7
  • 18
  • I tried so many things and this was all it was! Thanks for the tip. – Desmond Feb 03 '21 at 04:39
  • Yes, someone wrote this [exact answer in 2019](https://stackoverflow.com/a/57441556/542251). Please don't re-iterate answers – Liam Mar 04 '22 at 15:15
4

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

Kon
  • 27,113
  • 11
  • 60
  • 86
4

Closing PHPStorm fixed the issue for me.

Rualark
  • 445
  • 1
  • 5
  • 18
2

I did the following:

  1. Upgraded the npm to the latest version: npm install -g npm@6.5.0-next.0
  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.
RV.
  • 2,781
  • 3
  • 29
  • 46
2

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.

millenion
  • 1,218
  • 12
  • 16
2

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

Brenda Jimenez
  • 751
  • 1
  • 7
  • 12
  • I always use bash terminal and never had the problem before. Today i started to got the same error, on all terminals, after antivirus update – Davide Arcinotti Oct 24 '22 at 07:33
2

I came here with the same error, followed all the answers here, and none solved it. I started going through my package.json removing all suspicious packages. Removing this unnecessary one solved our problem. "npm": "^8.3.2"

Jnr
  • 1,504
  • 2
  • 21
  • 36
1

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.

SharpC
  • 6,974
  • 4
  • 45
  • 40
Web Steps
  • 334
  • 2
  • 11
  • `npm ERR! Refusing to delete C:\Program Files\nodejs\npm.cmd: is outside C:\Program Files\nodejs\node_modules\npm and not a link` – john k May 31 '23 at 19:23
1

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.

kmk09k
  • 324
  • 1
  • 5
  • 12
1

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

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.

Aryan
  • 1,767
  • 2
  • 22
  • 39
1

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

Ashutosh dwivedi
  • 510
  • 3
  • 16
1

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.

Lemon
  • 44
  • 4
1

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

ebram khalil
  • 8,252
  • 7
  • 42
  • 60
1

run the CLI as administrator always solve the problem for me

Khaled Boussoffara
  • 1,567
  • 2
  • 25
  • 53
1

I think it should be related to updating npm issue. As a workaround for now you can specify npm version as 8.3.1.

  - name: Install latest npm version
    run: npm install --global npm@8.3.1
anouar es-sayid
  • 355
  • 5
  • 11
1

I'm updating Angular 13 to 14 and I faced this issue. None of these solutions worked so I just restarted the laptop and the npm i --force worked! It seems like one process on the background is locking the access to certain node_modules files (you may have an ng serve running somewhere, or been launched by a git bash which you close but kept running on the background)

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
kingabdr
  • 588
  • 5
  • 12
0

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

UmaShankar
  • 221
  • 5
  • 14
0

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

Ramin Ar
  • 1,213
  • 13
  • 10
0

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

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.

user3544913
  • 81
  • 1
  • 1
0

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

Markus Hettich
  • 544
  • 6
  • 12
0

I closed VS, deleted the node_modules folder.

Then ran:

npm i -D -E clean-webpack-plugin@3.0.0 css- 
loader@3.4.2 html-webpack-plugin@3.2.0 mini-css- 
extract-plugin@0.9.0 ts-loader@6.2.1 typescript@3.7.5 
webpack@4.41.5 webpack-cli@3.3.10 

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.

Sam
  • 4,766
  • 11
  • 50
  • 76
0

If you are having the issue on PHP Storm:

  1. Close terminal tab
  2. Open a new terminal tab
2Fwebd
  • 2,005
  • 2
  • 15
  • 17
0

I was facing the same issue, Used npm cache clear command in cmd with open as Administrator. Close VScode and reopened it and it worked.

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
Analyst
  • 751
  • 6
  • 15
0
  1. I reinstall NodeJS
  2. Closed VSCode
  3. Open VSCode to where the project was to get it to run without issues
0

This simple remady works for me

  • Open PowerShell with un as administrator

enter image description here

  • Run this command

    npm i
    
Praveen Ramanayake
  • 313
  • 1
  • 2
  • 12
0

If you have tried all the methods suggested in this post, and still not able to work with it, then this might be just a permission problem in your folder. Simple go to your root directory, then right click and select properties. In the properties window select the security tab -> advanced settings and then give full control and save.

starball
  • 20,030
  • 7
  • 43
  • 238
-1

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.

-1

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.
Pramod
  • 768
  • 1
  • 12
  • 27
-1

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

simibac
  • 7,672
  • 3
  • 36
  • 48
-1

In my case, I had a Windows Explorer opened to the node_modules directory causing issue. Closing this did the trick.

java-addict301
  • 3,220
  • 2
  • 25
  • 37
-1

In my case it was the fact that I actually moved the project to Trash, re-cloned the project from git, but seems like Terminal was still pointing to the folder moved in Trash. I only had to cd back to newly cloned project, and everything worked just fine.

Beny Boariu
  • 736
  • 2
  • 11
  • 26
-1

In my case I was doing a fs.renameSync when this error happened.

It seems that the "new" path already exists and deleting it doesn't throw an error.

It doesn't really fix the issue but if you just wanna build the app just like I do, then this works.

I am L
  • 4,288
  • 6
  • 32
  • 49
-2

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

pallav bohara
  • 6,199
  • 6
  • 24
  • 45
-2

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.

magic_turtle
  • 1,243
  • 3
  • 17
  • 37