nodemon restart after file deletion does not restore application state
0 reputation · 07 Jun 2025, 04:15 UTC
0 reputation · 07 Jun 2025, 04:15 UTC
When nodemon detects a deletion of a watched file, it emits a restart event and spawns a new child process, but it does not provide a mechanism to run any user‑defined logic before that restart.
The goal is to ensure that critical application state can be verified or restored from a backup before the process is restarted, without having to modify nodemon’s source code.
Is there a way to configure nodemon to execute a user‑defined script before it restarts the child process on a file‑delete‑triggered restart?
Can a pre‑restart hook be added through existing nodemon options such as --exec or --signal without requiring a fork of the project?
What patterns do developers use to integrate backup verification with nodemon’s automatic restart behavior while preserving the tool’s simplicity?
29275 reputation · 07 Jun 2025, 13:11 UTC
Nodemon does not provide a built‑in option to run user‑defined logic before it restarts the child process when a watched file is deleted.
restart event and immediately spawns a new child process.--exec flag replaces the application process with the command you give; it does not run a separate preparatory step.--signal flag can send SIGTERM/SIGINT to the child process, allowing graceful shutdown handling, but the signal is not tied specifically to the file‑deletion event.To run verification or backup restoration before a deletion‑triggered restart, use one of the following patterns that do not require forking nodemon:
--exec – Create a small script that performs the backup check and then launches your app. Start nodemon with nodemon --exec "./start‑app.sh" node app.js. The wrapper runs on every restart (including deletion events) and can exit with a non‑zero code to abort the restart if verification fails.--signal SIGTERM. In your application, listen for process.on('SIGTERM', async () => { await verifyBackup(); process.exit(0); });. When nodemon restarts, it sends SIGTERM, giving your app a chance to run verification before exiting.chokidar or fs.watch) to run the backup script. On success, trigger nodemon’s restart by touching a dummy file or sending a signal (e.g., kill -SIGUSR1 $(pgrep -f nodemon)) if you have enabled a custom signal.Recommendations may vary with nodemon version. Please confirm the nodemon version you are using (e.g., nodemon -v) so we can verify that the --signal behavior described above applies to your release.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.