The Node process exit method to end a node process

So there is the question of 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 global.

When a process exits it often does so with an exit code. When using the main process.exit method it is possible to pass a code to let any additian scripts that might make use of what I am making know if the script exited as exspect with a code of zero, or if some kind of error happend with a code of one or higher. It is also possible to define some event handers that will fire when this process.exit method is called, or if for whatever the reason the process exits.

1 - Node process exit method basic example

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.

1
2
3
4
5
6
7
process.stdin.on('data', (data) => {
console.log(data);
})
process.on('exit', (code) => {
console.log('process exited with code: ' + code);
});
process.exit(0);

2 - The node process exit method can be used to end what would otherwise be an endless loop

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.

1
2
3
4
5
6
7
8
9
10
// keep looping until a condition is met
let loop = () => {
let t = setTimeout(loop, 100);
let rnd = Math.floor(Math.random() * 6) + 1;
console.log(rnd);
if (rnd === 6) {
process.exit(0);
}
};
loop();

3 - The process exit code property

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let rollCount = 0;
let roll = () => {
return Math.floor(Math.random() * 6) + 1;
};
let loop = () => {
let t = setTimeout(loop, 100);
let d1 = roll(),
d2 = roll();
console.log(d1, d2);
if ((d1 === 1 && d2 === 1) || rollCount >= 10) {
process.exitCode = 1;
clearTimeout(t);
} else {
if (d1 === 6 && d2 === 6) {
process.exitCode = 0;
clearTimeout(t);
}
rollCount += 1;
}
};
process.on('exit', (code) => {
console.log('process exited with code: ' + code);
console.log('roll count: ' + rollCount);
});
loop();

4 - Conclusion

The process.exit method is a method in the process global that can be used to end the current process with a given Exit code. Speaking of exit code there is also the process.exitCode property that cen be used as a way to set what the default exit code status should be for a process without actually ending the process when setting the propery. In addition there are a number of events to which handlers can be attached to define some custom logic tofire when the current process does in fact exit.

There is maybe a bit more to write about when it comes to exit codes, but it would seem that much of it might have to do with things that wil chnage a little from one operating system to another. These days I often work, and test on a Linux system, and when it comes to bash there is knowing a thing or two about the speshal paramaters that can be used to check the exit code status of the last process that ended. However that might be a matter for another post that has more to do with bash, and Linux, rather than nodejs scripts think might run in systems other than that of Linux.