nodemon: How to cleanly exit nodemon

We have a situation where we would like to completely quit node and nodemon.

We are using process.exit(0) to cleanly exit node. When this happens, we also want nodemon to quit. Instead it stays running and monitoring the directory.

How do we cleanly exit nodemon from within the app?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 13
  • Comments: 19 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I think the neatest way to implement this might be to let callers opt in to an exit code that would cause nodemon to quit. For example:

nodemon --kill-code 222 ...

Would cause nodemon to close if the child process exits with code 222.

(An alternative, more readable option name might be --die-on 222 but -d is already taken, as are -e and -x)

For anyone who stumbles on this thread in the future - there now appears to be an –exitcrash flag which will cause nodemon to exit if the child process crashes.

Firstly you’re commenting on an issue from 4 years ago (and closed).

Secondly, you can use the OS to do this without having to add to nodemon:

nodemon || exit 222

Starting the main script via a proxy seems to address the original question, how to make sure nodemon ends when the child process ends.

// run-main.mjs
import nodemon from 'nodemon';

const mainArgs = process.argv.slice(2).join(' ');
nodemon(`<nodemon arguments> extensions/main.mjs ${mainArgs}`);
let ignoreExit = false;
nodemon.on('restart', () => {
  ignoreExit = true;
}).on('exit', () => {
  if(ignoreExit) {
    ignoreExit = false;
    return;
  };
  process.exit();
});

This issue has been automatically marked as idle and stale because it hasn’t had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up. Thank you for contributing ❤️

I was looking for this too. I found a workaround that might be helpful.

If you exit with process.exit(2) it will cause nodemon also to exit. Nodemon will print stack trace so you might also hook into error event to make it clean, e.g.:

nodemon(...)
  .on('error', function(code) {
    if (code !== 2) {
      console.log('Exited with error code: ', code);
    }
  });

if you don’t want to or can’t modify the child process it should be possible to run it like your_command || exit 2 to make it work.

More details: https://github.com/remy/nodemon/issues/627