Exiting from Node.js: Different Methods with Examples

Node.js is a JavaScript runtime built upon the V8 engine that uses JavaScript for creating the server side. The server created using Node.js can interact with the operating system and file system. It can perform all types of non-blocking I/O operations. 

The Node.js server runs continuously to provide resources to the client, but sometimes it needs to exit to stop. There can be many reasons for Node.js server exiting such as some error occurring, some suspicious activity is detected or the system being upgraded.

In this tutorial, we will learn different approaches to exit in Node.js.

Methods to Exit in Node.js

Exit in Node.js means stopping the server or exiting all the running processes. In this tutorial we will cover two ways to exit in Node.js, first using keyboard shortcut keys and second using methods from the Node.js global process module. Let’s look at them one by one.

Using Command Line Shortcut

One of the quick and easier ways to exit Node.js using the command line is by pressing the below keyboard key shortcut.

ctrl + c

Example:

Here we have a Node.js server running, let’s press ctrl + c to see the result.

Server is running

Output:

Server Exit

In the above output, you can see we got exited from the Node.js server. It’s hard to see the demonstration, but you can try it quickly to verify the output.

Using Node.js Global Process Object

In Node.js, there are hundreds of processes running simultaneously and it is important to control them, the process object gives the power to do so. 

A process is a global object in Node.js. It is used to get details about the different processes running, used to manipulate them, and stop the processes.

We have covered Node.js process object and all its method in detail in another tutorial, NodeJS Process Object: 7 Process Methods in NodeJS to Know if you want to read.

Let’s now see the different methods of process modules to exit in Node.js.

process.exit()

The process global object in Node.js has a method exit() that can be directly used inside the program to exit Node.js. It is basically used to exit the current running process but we can also use it to stop the server as well.

Syntax:

process.exit();

Example:

In the below example, we have used a loop that runs endlessly, but we have a condition, that if the value of x is greeted than 10 then process.exit() statement will be executed, let’s see the result.

let x = 1;

while (x > 0) {
    console.log(x);
    x++;

    if (x > 10) {
        console.log('Exiting...');
        process.exit();
    }
}

Output:

1
2
3
4
5
6
7
8
9
10
Exiting...

In the output, you can see our server is stopped and we successfully exit Node.js.

process.abort()

This method can also be used to exit from Node.js. It immediately terminates all processes and exits the Node.js programs.

Syntax:

process.abort();

Example:

Let’s see the same example as above just change process.exit() to process.abort().

let x = 1;

while (x > 0) {
    console.log(x);
    x++;

    if (x > 10) {
        process.abort();
    }
}

Output:

1
2
3
4
5
6
7
8
9
10
 1: 00007FF6BA76041F napi_wrap+102143
 2: 00007FF6BA70EA06 uv_loop_fork+44230
 3: 00007FF6BA6B8379 v8::internal::StackGuard::ArchiveSpacePerThread+26233
 4: 00007FF6BACEC0A9 v8::internal::OFStreamBase::xsputn+5065
 5: 00007FF6BACEB5E8 v8::internal::OFStreamBase::xsputn+2312
 6: 00007FF6BACEB918 v8::internal::OFStreamBase::xsputn+3128
 7: 00007FF6BACEB73D v8::internal::OFStreamBase::xsputn+2653
 8: 00007FF6BB2A3916 v8::internal::NativesCollection<0>::GetScriptsSource+660214

The above output shows that we are successfully exited in Node.js.

process.kill()

Another process object method that can be used to exit Node.js.

Syntax:

process.kill(process.pid);

Example:

In the below example, we have to define that if the value of a variable is true then a string is printed in the console otherwise the Node.js gets exited.

const hello = false;
 
if(hello){
    console.log('Hello World!');
}else {
    console.log('Goodbye World!');
    process.kill(process.pid);
}
 
console.log("I will not execute.");

Output:

Goodbye World!

See, again Node.js is exited.

Summary

In this tutorial, we have explained how to exit in Node.js using a command line shortcut(ctrl + c) and using the Node.js global process object method. We hope this tutorial helps you by solving your dought. 

Reference

https://stackoverflow.com/questions/5266152/how-to-exit-in-node-js

Aditya Gupta
Aditya Gupta
Articles: 123