Summary
When using fs.watch on a directory with rapid file creation and deletion events, the callback sometimes receives incorrect event types or misses events entirely. Behavior differs across OS platforms (Windows/Linux).
Steps to Reproduce
- Create a script that uses
fs.watch on a test directory.
- Rapidly create/delete files in that directory (e.g., using a loop or CLI tool).
- Observe that:
- Some
rename events are not emitted.
change events are emitted unexpectedly.
- In rare cases, no events occur until another file operation triggers them.
Expected Behavior
fs.watch should emit event callbacks consistently for every create/delete operation.
Actual Behavior
Events are missed or reported incorrectly, especially under high-frequency changes.
Environment
- Node.js version: Latest Stable (please fill exact if required)
- OS: Windows 11 / Ubuntu 22.04 (issue observed on both)
- Reproduction code:
const fs = require('fs');
fs.watch('./test', (event, file) => {
console.log(event, file);
});