QA
Intermediate LevelNode JS

Why is native fs.watch() unreliable across platforms and why do build tools rely on Chokidar?

❓ Interview Prompt: Native fs.watch() behaves inconsistently across macOS, Linux, and Windows (duplicate events, missing rename notifications, high CPU). Chokidar normalizes OS events, debounces duplicates, and handles recursive directory watching.

Native 'fs.watch()' relies on OS kernel events (inotify on Linux, FSEvents on macOS, ReadDirectoryChangesW on Windows):

Limitations:

•
Emits duplicate events per save.
•
Missing recursive watching on Linux.
•
'Chokidar' wraps 'fs.watch', debouncing events, handling file locks, and normalizing cross-platform behaviors.

Code Implementation Example

javascript
import chokidar from 'chokidar';

// Cross-platform reliable file watching
const watcher = chokidar.watch('./src', { ignored: /(^|[/])../, persistent: true });
watcher.on('change', (path) => console.log('File changed: ${path}'));
💡Cross-platform file watching with Chokidar.
🎯 Interview Key Takeaway

Use Chokidar for file watching in build tools to avoid native fs.watch cross-platform bugs.

#chokidar#fs.watch#File Watching#Intermediate
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?