How to Exit in Node. Js
What Is the Command That Is Used to Exit? (I. E Terminate the Node. Js Process) 1 22 Answers Call the Global Process Object's Exit Method: Process. Exit() from...
What is the command that is used to exit? (i.e terminate the Node.js process)
22 Answers
Just a note that using process.exit([number]) is not recommended practice.
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 toprocess.stdoutandprocess.stderr.In most situations, it is not actually necessary to call
process.exit()explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. Theprocess.exitCodeproperty can be set to tell the process which exit code to use when the process exits gracefully.For instance, the following example illustrates a misuse of the
process.exit()method that could lead to data printed tostdoutbeing truncated and lost:// This is an example of what *not* to do: if (someConditionNotMet()) { printUsageToStdout(); process.exit(1); }The reason this is problematic is because writes to
process.stdoutin Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Callingprocess.exit(), however, forces the process to exit before those additional writes tostdoutcan be performed.Rather than calling
process.exit()directly, the code should set theprocess.exitCodeand allow the process to exit naturally by avoiding scheduling any additional work for the event loop:// How to properly set the exit code while letting // the process exit gracefully. if (someConditionNotMet()) { printUsageToStdout(); process.exitCode = 1; }
From the official nodejs.org documentation:
process.exit(code)
Ends the process with the specified code. If omitted, exit uses the 'success' code 0.
To exit with a 'failure' code:
process.exit(1);
If you're in a Unix terminal or Windows command line and want to exit the Node REPL, either...
- Press Ctrl + C twice, or
- type
.exitand press Enter, or - press Ctrl + D at the start of a line (Unix only)
From the command line, .exit is what you want:
$ node
> .exit
$
It's documented in the REPL docs. REPL (Read-Eval-Print-Loop) is what the Node command line is called.
From a normal program, use process.exit([code]).
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. A quote from documentation:
It is important to note that 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 toprocess.stdoutandprocess.stderr.In most situations, it is not actually necessary to call
process.exit()explicitly. The Node.js process will exit on it's own if there is no additional work pending in the event loop. Theprocess.exitCodeproperty can be set to tell the process which exit code to use when the process exits gracefully.
Let’s cover possible reasons why you might be willing to exit node.js process and why you should avoid process.exit():