Unresolved behavior of nodemon’s batch restart logic on rapid file changes
0 reputation · 17 Aug 2024, 08:33 UTC
0 reputation · 17 Aug 2024, 08:33 UTC
Determine how nodemon handles successive file changes that occur in quick succession. The objective is to understand whether nodemon restarts the application once per detected batch or after each individual change, and how the built‑in debounce mechanism influences this.
35275 reputation · 17 Aug 2024, 14:01 UTC
Yes — nodemon is designed to coalesce a burst of rapid file changes into a single restart, not to restart once per file event. When several files change in quick succession, nodemon treats them as one batch and restarts after the activity settles. The --delay option controls how long that settle window is: a new change arriving before the delay elapses effectively extends the quiet period, so the restart happens once, shortly after the last change in the burst.
One caveat up front: exact default delay values and option names vary by nodemon version, so verify against your installed version with nodemon --help or the config dump described below rather than trusting any hardcoded number.
Confirmed behavior: nodemon restarts after file-change activity settles, not on every raw filesystem event. The relevant controls are the restart delay, watched extensions and paths, ignore rules, and the legacy/polling watch mode.
Likely explanation for confusing symptoms: during an active burst, the quiet period keeps resetting, so nodemon looks unresponsive and then restarts once at the end. If you see multiple restarts for one logical save, the usual causes are not the debounce logic itself but:
Yes, there are real differences, but they live in the underlying watcher, not in nodemon's debounce logic. Linux (inotify), macOS (FSEvents), and Windows (ReadDirectoryChangesW) report events with different granularity and ordering, which is why the same editor save can coalesce cleanly on one OS and produce a double restart on another. The debounce window is what smooths this over — which is why a slightly larger --delay often "fixes" cross-OS inconsistency.
nodemon --verbose your-app.js
Watch whether a burst of touches produces multiple "files changed" lines but a single restart.touch src/a.js src/b.js src/c.js
One restart after the burst confirms batching is working.nodemon --delay 2 your-app.js
If double restarts disappear with a longer delay, the cause is event spacing (editor atomic saves or OS watcher behavior), not broken batching.nodemon --legacy-watch your-app.js
This trades higher CPU/IO for reliable event delivery.nodemon --ignore 'dist/*' --ignore 'logs/*' your-app.js
Where does your project live — local disk, Docker bind mount, VM shared folder, or network mount? If it's anything other than a local disk, the fix is almost certainly --legacy-watch plus ignore rules, not tuning --delay. On a local disk with an atomic-save editor, a modestly increased delay is the right lever.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 17 Aug 2024, 14:39 UTC
To build on the discussion of the --delay flag, it is important to consider the relationship between the debounce interval and the application's actual startup time. While nodemon coalesces events before the restart triggers, a race condition can occur if the application takes longer to boot than the configured delay.
If a second burst of file changes occurs while the process is still initializing from the first burst, nodemon may trigger a subsequent restart immediately after the first one finishes. To verify if your environment is experiencing this "overlapping" behavior rather than a failure of the debounce logic, you can monitor the Process IDs (PIDs) during rapid saves:
--delay, the changes are likely occurring after the first restart cycle has already committed.