Skip to content

watch mode should restart a script when it exiting with an error code like nodemon does #57657

Description

@doberkofler

What is the problem this feature will solve?

This is the default behavior in nodemon and 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.js using node --watch=fail.ts fail.js, the following output is generated

Exit with code 1
Failed running 'fail.js'

but the script is actually not restarted as I would have expected from the default behavior in nodemon that I'm trying to replace.

const exitCode = 1;
console.log(`Exit with code ${exitCode}`);
process.exit(exitCode);

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

Activity

  1. dario-piotrowicz commented on Apr 13, 2025

    @dario-piotrowicz
    Member

    Hi @doberkofler 🙂 I tried running your js file with node --watch and it seems like watching just works for me:

    57657.mp4

    am I misunderstanding/missing something?

    also see:

    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)}`,
    ]);
    });

  2. dario-piotrowicz commented on Apr 13, 2025

    @dario-piotrowicz
    Member

    Ah I see... sorry I missed this bit from your issue: node --watch=fail.ts fail.js, that's not how you should use watch, the watch flag does not take an argument as far as I know:

    Image

    did you mean to run node --watch --watch-path=fail.ts fail.js instead?

  3. doberkofler commented on Apr 15, 2025

    @doberkofler
    Author

    @dario-piotrowicz

    My 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();
    	}
    });
  4. dario-piotrowicz commented on Apr 18, 2025

    @dario-piotrowicz
    Member

    Hi @doberkofler, thanks for the extra context 🙂

    I copy-pasted your code into an index.mjs file and run it with both node --watch index.mjs and nodemon index.mjs I 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 e and 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.mjs then 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 🙂

  5. doberkofler commented on Jun 17, 2025

    @doberkofler
    Author

    @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!

  6. dario-piotrowicz commented on Jun 17, 2025

    @dario-piotrowicz
    Member

    @doberkofler no worries, I'm happy that your issue got solved 😄

    If there's anything else I can help with I'm always here 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions