process.exit()

Terminates the process synchronously with the given exit code.

Since Node 0.x Spec ↗

Syntax

process.exit([code])

Parameters

NameTypeRequiredDescription
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.

See also