Issue
This Content is from Stack Overflow. Question asked by RPaladin
I’m aware from this other StackOverflow answer that fs.watch()
fires twice on some operating systems (I’m on Windows 11). The weird thing though however, is that while fs.watch()
fires twice for me with a new setup and almost empty file, it fires three times for me when I’m running it in a complex/large script. Here is my complex script. I don’t believe that it’s a loop cycle issue, because 1) it returns console.logs
in my fs.watch()
function 2) it returns console.logs()
directly inside the function that should be run after fs.watch()
is triggered. For those interested I put a console.log()
between these lines.
To put it into summary, I’d like to know whether three pulses is expected behavior on top of stated two pulses, and if it’s not normal behavior, how to fix my code to restrict it only to two pulses, if possible.
Solution
This issue is described as expected behavior here.
What you see as "one" modification is really numerous calls to the Win32 API. Since there is no transaction to bucket all of the calls together, the filesystem is forced to send out multiple notifications for various kernel calls.
So when using fs.watch on Windows, expect multiple calls to be made for a single modification.
Chokidar is the only alternative I know of which does watching very well.
This Question was asked in StackOverflow by RPaladin and Answered by gear4 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.