Repository navigation
watch mode should restart a script when it exiting with an error code like nodemon does #57657
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Mar 28, 2025 Hi @doberkofler 🙂 I tried running your js file with
node --watchand it seems like watching just works for me:57657.mp4
am I misunderstanding/missing something?
also see:
node/test/sequential/test-watch-mode.mjs
Lines 271 to 286 in 4a4aa58
it('should watch changes to a failing file', async () => { const file = createTmpFile('throw new Error("fails");'); const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, completed: 'Failed running', shouldFail: true }); assert.match(stderr, /Error: fails\r?\n/); assert.deepStrictEqual(stdout, [ `Failed running ${inspect(file)}`, `Restarting ${inspect(file)}`, `Failed running ${inspect(file)}`, ]); }); Reacted by Dieter OberkoflerMy use case is a data gatherer that runs for a very long time and should:
- restart when the source or a configuration file changes and this is working as expected
- restart when the process exists with an error and this does not seem to work or I'm just not using it properly
In the following example, I would expect the script to restart when existing with an error the same way nodemon does.
import readline from 'node:readline'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: 'Enter command (q=quit, e=error): ', }); console.log('App started. Type "q" to quit or "e" to exit with error.'); rl.prompt(); rl.on('line', (input) => { const command = input.trim().toLowerCase(); if (command === 'q') { console.log('Exiting normally...'); process.exit(0); } else if (command === 'e') { console.error('Exiting with error...'); process.exit(1); } else { console.log('Unknown command. Use "q" to quit, "e" for error.'); rl.prompt(); } });
Hi @doberkofler, thanks for the extra context 🙂
I copy-pasted your code into an
index.mjsfile and run it with bothnode --watch index.mjsandnodemon index.mjsI cannot see any difference between the two in regards of the restarting behavior when errors are triggered, in both cases:- the process starts and it asks the user for their input
- if I submit
eand error is logged in the terminal (the process is not restarting here with either node watch or nodemon) - if I apply any changes to
index.mjsthen the process restarts, asking again for the user's input
In both cases the error does not trigger the process to restart right away, but it does restart the process as expected when there are changes to the file, which seems totally correct to me
The only minor thing that I noticed is that node watch logs
Failed running 'fail.js'without saying that it is waiting for file changes (which it is instead) while nodemon instead logs[nodemon] app crashed - waiting for file changes before starting...clearly stating that it is waiting for changes, is there any chance that the messaging here is creating confusion?So, anyways I would really love to help here but I am really not understand the issue you're seeing 😓 , if you could clear up my doubts I'd be more than happy to see how I can help 🙂
@dario-piotrowicz Sorry for my very late feedback and you are absolutely correct. I completely misunderstood my own setup actually using PM2 for the restart. Sorry!
@doberkofler no worries, I'm happy that your issue got solved 😄
If there's anything else I can help with I'm always here 😄
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsAwaiting Triage

What is the problem this feature will solve?
This is the default behavior in
nodemonand would allow to automatically restart a script (in my case a web server) when an error forces the script to exit.Example:
When running the sample file
fail.jsusingnode --watch=fail.ts fail.js, the following output is generatedExit with code 1 Failed running 'fail.js'but the script is actually not restarted as I would have expected from the default behavior in
nodemonthat I'm trying to replace.What is the feature you are proposing to solve the problem?
Either automatically restart the script when it exists with an error code when --watch is used automatically or add a specific cli option (--watch-restart-on-error) that enables this.
What alternatives have you considered?
Sticking with
nodemon