Nodemon vs Node.js Process: Avoiding Duplicate Writes During Rapid Restarts
0 reputation · 29 Apr 2021, 22:52 UTC
0 reputation · 29 Apr 2021, 22:52 UTC
Determine how to configure Nodemon so that the Node.js application does not produce duplicate log entries when the process restarts immediately after a file change.
Nodemon triggers a restart on every detected file event, and the underlying OS file‑watcher may emit multiple rapid events for a single edit. The application writes to a log file on each start, so overlapping writes can occur if the restart occurs before the previous write completes.
--delay flag on the likelihood of duplicate writes in fast development cycles?--ignore patterns to exclude files that frequently change during a restart, and how does that affect necessary restarts?34775 reputation · 30 Apr 2021, 07:52 UTC
To stop duplicate log entries when Nodemon restarts the Node.js process quickly, configure Nodemon to debounce restarts, exclude the log directory, and ensure the application shuts down gracefully.
Nodemon watches source files with chokidar and spawns a new child Node.js process on each change. If several file‑system events arrive in quick succession, the old process may still be writing to a log file when the new process starts, causing overlapping writes and duplicate lines.
--delay (or --legacy-watch) introduces a debounce period that lets the previous process exit before a new one is spawned.--ignore pattern for the log directory prevents Nodemon from triggering a restart when the application writes to its own log file.nodemon --delay 2.0 src/index.js.nodemon --delay 2.0 --ignore 'logs/**' src/index.js.const fs = require('fs'); const logStream = fs.createWriteStream('logs/app.log', { flags: 'a' }); process.on('SIGTERM', () => { logStream.end(() => process.exit(0)); }); // normal logging … logStream.write(message + '\n');If you cannot modify the application to handle SIGTERM, rely solely on the debounce and ignore options; increase the delay until duplicate lines disappear in your test.
Missing diagnostic detail
Can you modify the application to listen for SIGTERM and flush its log streams? Knowing this determines whether the graceful‑shutdown step is required or if the debounce/ignore approach alone will suffice.
Use comments to ask for clarification. Post a solution as an answer.
6,510 reputation · 30 Apr 2021, 05:56 UTC
While --delay and --ignore mitigate the frequency of restarts, it is important to clarify why duplicate writes occur at the system level. By default, Nodemon often relies on SIGUSR2 to signal the child process to restart. Because Node.js handles I/O asynchronously, a write operation initiated just before the signal is received may still be pending in the event loop.
If the previous process hangs during shutdown or takes longer to terminate than the debounce window, you end up with a race condition where two separate PIDs are attempting to append to the same file handle. To verify if this is happening in your environment, you can run ps aux | grep node during a rapid save loop to check for overlapping processes.
top or htop to see if multiple Node processes persist during restarts.process.on('SIGTERM') vs. the application start time to measure the actual overlap window.