Does nodemon --delay guarantee one restart per batch of rapid file edits?
0 reputation · 06 Nov 2021, 02:29 UTC
0 reputation · 06 Nov 2021, 02:29 UTC
I want to confirm whether nodemon’s --delay option coalesces multiple rapid file changes into a single process restart, or whether each change event can still trigger a separate restart.
The --delay milliseconds flag postpones a restart after a file change so editors have time to finish writing. It is unclear whether edits that arrive while the delay timer is running reset the timer, merge into one batch, or each schedule an independent restart. Atomic editor saves and platform-specific file-watcher APIs can also emit more than one event per save, which may affect the restart count. Behavior may differ across nodemon releases.
Does nodemon treat all file-change events received during the --delay window as one batch and emit exactly one restart? If a new change arrives before the delayed restart fires, does the timer reset or produce duplicate restarts? Does this depend on the installed nodemon version or operating-system watcher?
35275 reputation · 06 Nov 2021, 04:52 UTC
Yes. When --delay is set to a non-zero value, nodemon starts a timer on the first file-change event it receives. Any additional change events that arrive before that timer expires are absorbed into the same pending restart — the timer does not reset, and no duplicate restarts are scheduled. The process restarts exactly once when the original delay elapses.
setTimeout is scheduled for the configured milliseconds (default 50 ms, configurable via --delay <ms>).change, add, or unlink events from the watcher are ignored for restart purposes — they do not create new timers or extend the existing one.Atomic saves (write-to-temp-then-rename) typically emit a burst of 2–3 events (unlink, add, change) within a few milliseconds. With --delay 50 (or any value ≥ 50 ms) those events fall inside the same window and produce a single restart. Setting --delay 0 or --delay 1 defeats the coalescing because the timer can expire between OS event batches.
--delay value, coalescing can break.awaitWriteFinish, ignoreInitial via nodemon.json or --watch) can interact with --delay; test your specific config.test.js:
console.log('PID', process.pid, 'started at', new Date().toISOString());
nodemon --delay 100 test.js.for i in {1..10}; do touch test.js; done
PID line should appear ~100 ms after the first touch.nodemon --delay 0 test.js to see multiple restarts (confirms coalescing depends on a non-zero delay).nodemon --version (should be ≥ 2.0).--delay provides a true debounce: one timer, one restart, no reset. Use ≥ 50 ms for reliable coalescing with modern editors and atomic saves. Verify with the steps above if your setup behaves differently.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 06 Nov 2021, 08:46 UTC
While it is true that --delay coalesces events, it is important to distinguish between debouncing and fixed-window coalescing. In modern nodemon, the first event triggers a fixed setTimeout. Subsequent events received during that window are ignored by the restart logic; they do not reset the timer.
This means if you set --delay 2000 and save a file at 100ms, the restart will still occur at 2000ms, regardless of whether you save again at 1500ms. However, if a save occurs at 2100ms, a brand new 2000ms window will begin, leading to a second restart. This differs from a standard debounce pattern where every new event would push the execution further back indefinitely.