54

The error occurs after following steps in windows:

  1. Open one terminal: npm run start:dev
  2. Open another terminal: ng build --watch

However, it works in Mac by using the above commands. Part of the package.json configuration are:

"scripts": {
"ng": "ng",
"start:dev:prod": "ng build --prod && cd dist/ && node static/server.js",
"start": "node static/server.js",
"build:prod": "ng build --prod",
"test": "ng test --code-coverage",
"lint": "ng lint",
"e2e": "ng e2e",
"start:dev": "npm run build & cd dist/ & SET APP_ENV=dev & node static/server.js",
"build": "ng build"
},

Error log:

Error: EBUSY: resource busy or locked, rmdir 'C:\AngularProject\dist'
Error: EBUSY: resource busy or locked, rmdir 'C:\Spring-Intern-Projects\wdpr-accounting-reference-client\dist'
at Object.rmdirSync (fs.js:701:3)
Liam
  • 27,717
  • 28
  • 128
  • 190
Xiaohua Cao
  • 649
  • 1
  • 5
  • 4
  • 7
    The simplest thing you can try is closing all your terminals to get rid of a blocker and trying again. – Mike Poole Nov 05 '19 at 15:58
  • Yes. Stopped AVG and it runs fine now. – Erik Apr 18 '20 at 16:10
  • 1
    Because I'm cd'd into the directory I'm running the command, the directory will forever be busy. I can't run the command without a shell open. – Kirk Ross Sep 13 '20 at 23:41
  • My issues was also my Anti-Virus software, once I switched that off, my install worked fine. Obviously switched it straight back on again! – jackofallcode May 27 '22 at 12:48

18 Answers18

105

If all the above solutions did not work AND you are using windows PC like in my case, the easiest thing to do is:

  1. Press windows key plus R on your keyboard to open the run command box and type this

    resmon.exe

  2. click OK to open resource monitor

  3. In the monitor, navigate to CPU tab

  4. Under Associated Handles in the search box, copy the path to the file or folder in question and paste it in there

  5. Click on the search icon

  6. You should be able to see all the programs using the file or folder

  7. right-click each and end-process

enter image description here

Now, continue what you were trying to do on the file or folder. In my case I was having problems using react-create-app via npm. More information here #117908

I just hope it helps someone.

Nicholas Mberev
  • 1,563
  • 1
  • 14
  • 14
  • 1
    my problem was a few locked cache folders. I used your method to kill the git process locking them and deleted the folders. I had to wait a few seconds with the console looking like it's stuck until it just resumed the npm install process on its own after a while. – Lesh_M Sep 29 '21 at 08:13
  • @OmerLeshem I am glad it helps. It looks like the issues is more related to Windows. I have not experienced it on Mac OS. – Nicholas Mberev Sep 29 '21 at 15:55
14

This issue due to cache, run the below command in terminal

npm cache verify
Balaji D
  • 1,245
  • 13
  • 20
14

For VsCode users:

Check if the directory/folder you are trying to delete is opened in the terminal(s). There might be multiple terminal instances, so check if you have any.

Changing the directory opened in the terminal or killing that terminal instance will solve the issue.

If none of these works close the vscode and try again. Thanks @Vugar Abdullayev

Lenzman
  • 1,177
  • 18
  • 30
10

The main reason you are seeing this error is due to a program putting a lock on the folder for whatever reason. Testing the command while closing programs one at a time can help resolve the issue and let you know which program is the culprit.

One issue people were having was with anti-malware on windows. You can read through this thread here:

https://github.com/npm/npm/issues/13461

In case link stops working here was the most upvoted comment:

In Windows 10 64-bit OS, I have resolved this issue by uninstalling Anti-Malware software.

Another way you can trigger this error is if you are running an npm run [command] while cd'd into the directory that its altering. This happened to me where I closed every program and it didn't work, but realized I was inside the dist folder which needed to be removed and rebuilt. This triggered the Error: EBUSY: resource busy or locked, rmdir error in PowerShell and can be reproduced every time.

JoeMoe1984
  • 1,912
  • 3
  • 21
  • 31
7

This is happend by overwrite some npm so it locked your source

solutions(what i did npm cache clean is also not working then i deleted package-lock started working)

  1. simply delete package-lock

  2. clean cache.

    npm cache clean

    also clear cache(optional)

  3. npm install --cache

Lenzman
  • 1,177
  • 18
  • 30
Balaji
  • 9,657
  • 5
  • 47
  • 47
4

I had a same problem and found the reason - the project is inside dropbox folder that synchronize and locke the node_modules folder. I closed dropbox and it worked.

Alex Sorkin
  • 101
  • 3
  • You literally just saved my monitor from getting a keyboard jammed through it. Can't thank you enough! – 1owk3y Nov 10 '22 at 15:57
  • I just had the same problem using zx, a node based helper for writing bash scripts - the easy solution was to stop syncing that folder – Brad Parks Jan 06 '23 at 22:52
3

I was able to solve my killing the npm and Java processes. I was having issue running my Cordova run android, so killing npm and java processes helped me solve the issue.

JMeter Dude
  • 209
  • 4
  • 17
1

You can delete following folder, then you can try again. Worked for me.

C:\Users\{username}\AppData\Local\Temp\npm-xxxxxxxxxx\
Adriaan
  • 17,741
  • 7
  • 42
  • 75
ufukcam
  • 93
  • 6
0

Restarting the system fixed the issue for me.

Arun Kumar
  • 135
  • 2
  • 4
0

Using Jest, and after upgrading to Angular 13, I started to get this error message.

I found out it was due to failed unit tests that still were using (incorrectly) Jasmine syntax. For example:

spyOn() // instead of jest.spyOn()
expect().and.returnValue(X) // instead of expect().mockReturnValue(X)

Once I updated my syntax to be "pure Jest" instead of Jasmine/Jest mixtures, these errors were resolved, and the EBUSY error was no longer thrown.

CSSBurner
  • 1,565
  • 15
  • 13
-1

Sometimes (in other case) its just a network problem, what im doing just using VPN, and its work.

Zulfikar Ahmad
  • 407
  • 1
  • 7
  • 18
-1

In my case android studio and VSCode was open. Closing android studio helped.

mahmood
  • 369
  • 3
  • 8
-1

It also occurs to me when I want to delete my data.db file when running and playing around with a simple python application that I made (app.py) to test the connection with my sqlite3 db. When I want to delete the .db file, my app.py is still running in the terminal, and the error pops up.

So, what I do is I kill/stop the terminal that runs the app.py. After stopping the running terminal, I can delete the .db file successfully.

*This is on windows PC.

-1

I tried deleting node modules and re-installing them but it didn't work. The issue in my case was because of a wrongly imported dependency by VSCode, I fixed the issue by removing the last imports I added to my code.

Shadab Ahmed
  • 556
  • 9
  • 20
-2

Just close the terminal and open it again

Eric
  • 75
  • 2
  • 5
  • Hey, looks like this is not a proper answer, please be more specific for an answer, thanks. – Hasip Timurtas Feb 02 '22 at 00:54
  • 1
    Often times a resource is locked only within the scope of a terminal. When you open a new one you start off with default settings once more. That's how it can solve those kind of issues but not always. – Eric Feb 02 '22 at 13:44
-2

It usually happens when the folder is opened in the terminal, after stopping the terminal i was able to delete the folder.

Siphamandla Ngwenya
  • 2,696
  • 1
  • 16
  • 21
-2

I solved my problem by granting the permissions to the relevant locked folder.

RenaudC5
  • 3,553
  • 1
  • 11
  • 29
-4

node_modules folder contents can make bad sectors on the drives, using defrag and optimiziton software for this drives can reduce problems like this.

Pedroxam
  • 27
  • 1
  • 8