What are exit codes in Node.js and what do they indicate?
Exit codes are numeric values returned by a Node.js process when it terminates. They tell the OS (or parent process) whether the process exited successfully or due to an error.
0— success, normal exit.Non-zero — some kind of error or abnormal termination.
// Preferred: set exit code and let process exit naturally
process.exitCode = 1;
// Forceful exit (use sparingly)
if (criticalError) {
process.exit(1);
}
// Check exit code in shell after running
// $ node app.js
// $ echo $? // prints the exit codeSetting process.exitCode allows the event loop to finish pending operations before exiting with the specified code. process.exit(1) forces immediate termination. The shell command echo $? shows the exit code of the last executed process, which is useful for CI/CD pipelines and scripts.
Know the difference between process.exit() and process.exitCode. The preference for exitCode over exit() shows you understand graceful shutdown practices.