

And some of them might be non-idempotent (UPDATE, DELETE). process.exit() will be a bad option here because it’s going to interrupt all requests which are in pipeline. Unlike command line script, express app keeps running infinitely, waiting for new requests. Case 2 - Termination because of external signal (SIGINT/SIGTERM/other)įor example, if you’re willing to gracefully shut down an express app. And when you settle this, you can use process.exitCode to return any result to calling process. It’s better to try to understand what is holding your script from exiting in expected way. It’s wrong to force process termination with process.exit() at this point. If script has reached its end and node interpreter doesn't exit, it indicates that some async operations are still pending. Let’s cover possible reasons why you might be willing to exit node.js process and why you should avoid process.exit(): Case 1 - Execution complete (command line script) If there is no additional work pending in the event loop. The Node.js process will exit on it's own

Including I/O operations to process.stdout and process.stderr. Process to exit as quickly as possible even if there are stillĪsynchronous operations pending that have not yet completed fully, It is important to note that calling process.exit() will force the It depends on the reason why you're willing to exit node.js process, but in any case process.exit() is the last option to consider.
#Commander one with nothing kill yourself how to#
Scheduling any additional work for the event loop: // How to properly set the exit code while letting Process.exitCode and allow the process to exit naturally by avoiding Rather than calling process.exit() directly, the code should set the Process to exit before those additional writes to stdout can be

Calling process.exit(), however, forces the Sometimes asynchronous and may occur over multiple ticks of the Problematic is because writes to process.stdout in Node.js are Truncated and lost: // This is an example of what *not* to do: Process.exit() method that could lead to data printed to stdout being Process.exitCode property can be set to tell the process which exitĬode to use when the process exits gracefully.įor instance, the following example illustrates a misuse of the There is no additional work pending in the event loop. The Node.js process will exit on its own if In most situations, it is not actually necessary to call Have not yet completed fully, including I/O operations to Possible even if there are still asynchronous operations pending that Just a note that using process.exit() is not recommended practice.Ĭalling process.exit() will force the process to exit as quickly as
