We use process.exit to stop a Node.js program immediately. Exit codes tell the system if the program ended well or had a problem.
process.exit and exit codes in Node.js
process.exit([code])
The code is a number. 0 means success, any other number means an error.
If you don't give a code, it defaults to 0.
process.exit()
process.exit(1)process.exit(42)This program prints a start message. If you run it with --fail argument, it prints an error and exits with code 1. Otherwise, it prints a success message and exits with code 0.
console.log('Start program'); if (process.argv.includes('--fail')) { console.error('Error: Something went wrong!'); process.exit(1); } console.log('Program finished successfully'); process.exit(0);
Always use process.exit carefully because it stops the program immediately, skipping any remaining code.
Exit codes help other programs or scripts know if your program worked or failed.
Use 0 for success and positive numbers for errors. Negative numbers are not recommended.
process.exit stops a Node.js program immediately.
Exit codes tell if the program ended well (0) or had an error (non-zero).
Use exit codes to communicate status to the system or other programs.