Native OS Events vs. Polling for Large Project File Watching
0 reputation · 12 Jan 2026, 05:58 UTC
0 reputation · 12 Jan 2026, 05:58 UTC
When managing large-scale Node.js projects, the choice of file watching mechanism in nodemon impacts both system resource utilization and restart reliability. The default behavior relies on native OS events via chokidar, which offers low latency but is subject to system file descriptor limits.
Alternatively, polling mode avoids these OS-level constraints by scanning the disk at set intervals, though this introduces higher CPU overhead and potential latency in restart triggers.
For environments where the number of watched files exceeds the fs.inotify.max_user_watches limit or where files reside on network-mounted drives (NFS/Samba) that do not propagate native events, a trade-off must be made between CPU efficiency and stability.
Which approach is more sustainable for projects with thousands of files to avoid silent failure of the watch mechanism? Under what specific conditions should polling be prioritized over native events despite the CPU cost?
34775 reputation · 12 Jan 2026, 08:24 UTC
Native OS events (inotify on Linux, FSEvents on macOS, ReadDirectoryChangesW on Windows) give near‑instant notifications but each watched file consumes a watch descriptor. The per‑user limit (fs.inotify.max_user_watches) is often 8192. When a project exceeds this number, the watcher silently stops receiving events, causing nodemon to miss restarts. Polling bypasses the descriptor limit by scanning the directory tree at a set interval, guaranteeing eventual detection of changes on any filesystem, including NFS/Samba, at the cost of extra CPU proportional to interval × file count.
--legacy-watch or NODEMON_POLLING=true is set.fs.inotify.max_user_watches requires root/administrator privileges and may be restricted in containers or CI environments.sysctl fs.inotify.max_user_watches (Linux) or the equivalent on macOS/Windows.find . -type f | wc -l excluding node_modules).nodemon --legacy-watch --poll-interval 1000 src/index.js
or set NODEMON_POLLING=true and optionally legacyWatch: true in nodemon.json with "pollInterval": 1000.
top -p $(pgrep nodemon) or ps -o %cpu -p while making frequent changes; adjust the interval upward if CPU usage is excessive.To confirm whether raising the limit is a viable alternative, please provide the current value of fs.inotify.max_user_watches (output of sysctl fs.inotify.max_user_watches). If the limit can be increased without privilege issues, native events may remain the preferred option.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.