Process (Node)

Why not to use process.exit(1)

process.exit(1) will quit without gracefully ending exiting processes. To allow a graceful exit, use process.exitCode(1). This will allow ongoing processes to finish and then send exit code 1 back to the executing shell.

// Bad

const { exit } = require('node:process');

// This is an example of what *not* to do:
if (someConditionNotMet()) {
    printUsageToStdout();
    exit(1);
}

// Good

const process = require('node:process');

// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
    printUsageToStdout();
    process.exitCode = 1;
}

References

  1. Exit Code > Process | Node.js v21.2.0 Documentation

Last modified: 202401040446