How to exit the program in Node.js?

When fatal error happened, we’d like to kill the program. How to exit the program in Node.js?

You can use:

process.exit()

Here, “process” is a global process object. exit method ends the process.

process.exit([code])

Ends the process with the specified code. If omitted, exit uses the
‘success’ code 0.

To exit with a ‘failure’ code:

process.exit(1);

The shell that executed Node.js should see the exit code as 1.

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *