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?
+1
Would like to know this as well.
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
In tests, I cannot fully kill a nodemon process. emit('quit') only triggers the listener(s) and:
nodemon(...).on('quit', function(){ this.reset() })
causes:
Assertion failed: (0), function uv_close, file ../deps/uv/src/unix/core.c, line 173.
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 <3
There has been some debate about removing the quit behaviour on exit code 2.
So I would not recommend this technique, if you can find an alternative way.
_Edit:_ We actually did remove it!
What is the state of this?
When process.exit(2) I get
[nodemon] app crashed - waiting for file changes before starting...
then I have to Ctrl+C a second time to actually kill it.
1- Is it the expected behavior?
if yes, is a app crashed red message really appropriate? Ctrl+C is not a "crash" per se.
2- Any way to nicely quit everything in one key stroke (not twice) and without a scary crash message?
Thanks!
@OoDeLally exit(2) is a crash code (from your script), so if you're catching the exit code inside your program and using exit 2, then nodemon is behaving exactly as it should.
A clean exit code is 0, anything above is a bad exit code, and nodemon will catch it.
Also, this issue was raised 3 years ago, so is unrelated to your original question.
Thank you @remy. process.exit(0) indeed nicely quits the nodeJS process, but not nodemon.
Is there a way to gracefully quit both nodejs and nodemon at once with Ctrl+C, given that I run nodemon with --no-stdin?
I am able to capture Ctrl+C in nodeJS, but after that, I am unable to gracefully quit both.
If I understood @edwardhorsford right, this is exactly what he originally asked, so that is why I am posting here.
Thank you
Quitting nodemon is done using ctrl-c (if you're capturing stdin - then
it's ctrl-c twice). nodemon isn't automatically exited (wasn't sure if
that's what you were asking, but it shouldn't exit as it's meant for
development).
On Mon, 9 Dec 2019 at 19:45, Pascal Heitz notifications@github.com wrote:
Thank you @remy https://github.com/remy. process.exit(0) indeed quit
nicely the nodejs process, but not nodemon.
Is there a way to gracefully quit both nodejs and nodemon at once, given
that I do capture stdin.
Thank you—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/remy/nodemon/issues/751?email_source=notifications&email_token=AAADLBEZJ5T3LCRVVOZZXS3QX2N5FA5CNFSM4BYI5M7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGKNTLQ#issuecomment-563403182,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAADLBHEFQGET3KKO3A36XTQX2N5FANCNFSM4BYI5M7A
.
@remy it's some years since I raised this (and I no longer work for that bit of gov that used it).
My use case was where the app's code has determined that nodemon should be quit. How should the app go about quitting nodemon? I think we had a situation where we asked the user a question in the terminal, and if they responded negatively, we then wanted to fully quit - at the time, we couldn't work out how to quit nodemon programatically.
Darn, I didn't realise I was commenting on a 3 year old issue (I was doing it via email).
Bottom line: unless you require nodemon as a dep, nodemon will not exit. This is expected behaviour.
It may be an old issue but one that is still relevant. I run a number of daemons using nodemon and they don't have a command line. When I run directly under node I can tell the program to exit itself but how do I do that with nodemon?
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)
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
I know it's an old issue. But it's not clear that it has been resolved. Note that I'm using Windows.
In the meantime I've taken a different approach that doesn't depend on nodemon.
It seems strange to me that nodemon doesn’t provide a clean, programmatic way to exit itself (in case it does, I can’t find it in the documentation).
I’ve since been using babel-node with better results.
Ok, what's the "..." guys? I can't seem to get this done using:
nodemon = require("nodemon");
nodemon()
.on('error', function(code) {
if (code !== 2) {
console.log('Exited with error code: ', code);
process.exit(code)
}
});
process.exit(99)
I get:
/home/owner/git/node-ticket-manager/node_modules/nodemon/lib/nodemon.js:37
if (settings.verbose) {
^
TypeError: Cannot read property 'verbose' of undefined
The surrounding code is lib/server.js in: https://github.com/poikilos/node-ticket-manager
Most helpful comment
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:
Would cause nodemon to close if the child process exits with code 222.
(An alternative, more readable option name might be
--die-on 222but-dis already taken, as are-eand-x)