How to terminate a Node.js program

Hey programmers. In this tutorial, I will explain to you how can we terminate a Node.js app. There are two methods explained in this tutorial.

Method 1: Using the ctrl+C key

When running Node.js programming in the console you can press ctrl+C to terminate the program.

Method 2: Using process.exit() function

We can programmatically exit the process using the process.exit() function which is provided by the process module. When this line runs, the process is forced to terminate. Any request sent to a server or callbacks or writing on the console will terminate forcefully.

The syntax for the same is –

process.exit(code)

Here code is a placeholder that can be replaced by any code you want the process to terminate with. If omitted this value is assumed to be ‘0’ which signifies success. To exit with a failure we can replace code with 1.

You may also learn,

Leave a Reply

Your email address will not be published.