I noticed that both process.exit()
and process.abort()
both stop a script. What are the differences between the two besides that one logs Aborted
?
process.abort()
stops the process immediately.
process.exit([exitCode])
method instructs Node.js to terminate the process as quickly as possible. You can also specify an exit code.
For exit codes:
0
means the process was exited successfully. 1
means it ended abnormally.0
is the default value.Calling process.exit()
will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.
The proper recommended way to exit from process is to set the exit code and allow process to exit naturally because calling process.exit()
forces the process to exit before any additional writes to stdout can be performed.
process.exitCode = 1;
If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().
The process.abort()
method causes the Node.js process to exit immediately and generate a core file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With