So there is the question pf how to make a node process exit when it does not do so normally, and also how to go about setting some events that will fire when the process exits. In this post I will be going over the process exit method and well as exit codes, exit events, and other related topics that mainly have to do with the use of the process module.
So if a process does not exit as expected or for whatever the reason I want to stop, end or otherwise exit a process in nodejs there is the exit method of the process global that can be used to do so. The method accepts one argument that is the exit code to set for the process.
So most of the time the use of the process exit method is not required, often by default the process will just end if there is nothing left to do. However in some situations it is required in the body of code to use it, such as in a loop that involves the use of setTimeout. Maybe it is not the best example as the clearTimeout method could also be used, but still if a condition occurs that is desired to end the process then the process exit method can be used as a way to do so.
The process exit method can be used as a way to end the process right away, but there is also the process exit code property that can be used as a way to set a code that will be handled in different ways when the process ends as normal.