process.exit()
Terminates the process synchronously with the given exit code.
Syntax
process.exit([code]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
code | integer | No | The exit code (default 0). Non-zero signals failure. |
Returns
never — Does not return; the process ends.
Examples
if (!process.env.API_KEY) {
console.error('API_KEY is required');
process.exit(1);
}
Output
API_KEY is required
// Prefer setting the code and letting Node exit naturally
process.exitCode = 2;
console.log('will exit with 2 after the event loop drains');
Output
will exit with 2 after the event loop drains
Notes
`process.exit()` forces an immediate exit and can truncate pending
stdout writes and async work. Prefer setting `process.exitCode` and
letting the event loop drain naturally; only call `exit()` for hard
failures.