In node.js applications i saw usage of both these methods process.exit(1)
and process.exit(0)
. Can anybody give me the exact answer ?
Exit code 1 is for when unhandled fatal exceptions occur that was not handled by the domain. process. exit(1); process. exit() is one of the methods for Node.
The process. exit() method is used to end the process which is running at the same time with an exit code in NodeJS.
Node normally exits with code 0 when no more async operations are pending. process. exit(1) should be used to exit with a failure code.
exit function exits from the current Node. js process.
Node normally exits with a 0 status code when no more async operations are pending. There are other exit codes which are described below:
1 - Uncaught Fatal Exception: There was an uncaught exception, and it was not handled by a domain or an uncaughtException event handler.
2 - Unused: Reserved by Bash for built in misuse.
3 - Internal JavaScript Parse Error: The JavaScript source code internal in Node's bootstrapping process caused a parse error. This is extremely rare, and generally can only happen during the development of Node itself.
4 - Internal JavaScript Evaluation Failure: The JavaScript source code internal in Node's bootstrapping process failed to return a function value when evaluated. This is extremely rare, and generally can only happen during the development of Node itself.
5 - Fatal Error: There was a fatal unrecoverable error in V8. Typically, a message will be printed to stderr with the prefix FATAL ERROR.
6 - Non-function Internal Exception Handler: There was an uncaught exception, but the internal fatal exception handler function was somehow set to a non-function, and could not be called.
7 - Internal Exception Handler Run-Time Failure: There was an uncaught exception, and the internal fatal exception handler function itself threw an error while attempting to handle it.
8 - Unused
9 - Invalid Argument: Either an unknown option was specified, or an option requiring a value was provided without a value.
10 - Internal JavaScript Run-Time Failure: The JavaScript source code internal in Node's bootstrapping process threw an error when the bootstrapping function was called. This is extremely rare, and generally can only happen during the development of Node itself.
11 - Invalid Debug Argument: The
--debug
and/or--debug-brk
options were set, but an invalid port number was chosen>128 - Signal Exits: If Node receives a fatal signal such as
SIGKILL
orSIGHUP
, then its exit code will be 128 plus the value of the signal code. This is a standard Unix practice, since exit codes are defined to be 7-bit integers, and signal exits set the high-order bit, and then contain the value of the signal code.
Source: https://www.tutorialspoint.com/nodejs/nodejs_process.htm
You can find the answer to your question in the documentation: https://nodejs.org/api/process.html#process_process_exit_code
Basically if you want to exit with success use 0 if you want to exit with failure use 1.
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