unicorn/no-process-exit

Disallow process.exit()

process.exit() stops the process immediately. Pending work is dropped, finally blocks don't run, and the code that called yours never gets a chance to handle the failure. Throw an error instead, or set process.exitCode and let the process end when it has nothing left to do.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-process-exit" = "error" # or "warn"

Examples#

example.js1 problem
function loadConfig(path) {
  if (!exists(path)) {
    console.error("config not found");
    process.exit(1);
  }
}
example.jsNo problems
function loadConfig(path) {
  if (!exists(path)) {
    throw new Error("config not found");
  }
}

The rule allows process.exit() in three places. The first is a file that starts with #!. That's a command-line program, so it decides when to exit:

example.jsNo problems
#!/usr/bin/env bun
if (!(await run())) {
  process.exit(1);
}

The second is a handler passed to process.on() or process.once():

example.jsNo problems
process.on("SIGINT", () => {
  closeServer();
  process.exit(130);
});

The third is a file that imports something from node:worker_threads, where process.exit() ends the worker thread, not the process:

example.jsNo problems
import { parentPort } from "node:worker_threads";

parentPort.on("message", message => {
  if (message === "stop") process.exit(0);
});