How to exit in Node.js

There are many various ways to exit a Node.js application.

When running a program in the console you can close it with ctrl-C & exit(), but what we are want to about discuss here is programmatically exiting.

Let’s get start with the most useful one, and see why you’re better good off not using it.

The process core module provides a handy method that allows you to programmatically exit from a Node.js program: process.exit().

When Node.js using runs this line, the process is immediately forced to exit.

Letting Start Now Script Exit Implicitly:

If this is fine coding for you, you can use pass an integer value that the signals the use code editor:

You can see this by running now the following script:

// index.js
console.log('Lara Tuto');

process.on('exit', function(code) {
    return console.log(`Hello I am exit now. ${code}`);
});

Run the code program with the command node index.js, and you will see it output the first statement about Index. In addition, the “exit” callback fires, getting in a print out of the message open terminal log, and an code is exit. You should see something similar to the following:

$ node index.js 
Lara Tuto
Hello I am exit now. 0

And last of the use of the below script:

// index.js

// ...

setInterval((function() {
    return console.log('My page is laratuto.com!');
}), 2000);

Run repeat. And because the repeating setInterval function blocks Node.js from exiting.and show the below output:

$ node index.js 
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
......

“My page is laratuto.com!” phrase is repeative log in the terminal until you forcefully exit, shortcut is Ctrl + C, or close your terminal.

People Ask For This

How do I exit a node js program?

How do I exit node js in terminal?

What is process exit in node JS?

How do you exit a JavaScript program?

Using process.exit() now solution below this headers…

We can now exit Node.js code editor using the explicit process.exit function call. The process.exit function exits from the current Node.js code editor. It takes an exit code..

This new code about of the get 4 second around calling and useful tips..

// index.js



setTimeout((function() {
    return process.exit(11);
}), 4000);

After run this code and printing after 4 second. and get this with 11 code

node index.js 
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!

About to exit with code 11

Old Node.js using process.kill() ..

To kill the main Node process, we just running in the past process:

To see this in exit this operation, replace the setTimeout function in our using previous code example this uses of process.kill.

// index.js



setTimeout((function() {
    return process.kill(process.pid);
}), 5000);

This code may be runned after 15 second. Because this code explained only is the name of the.

$ node index.js 

My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
My page is laratuto.com!
Terminated: 15

Was Node JS this article helpful?

How To Exit In Node.Js
How To Exit In Node.Js
When running a program in the console you can close it with ctrl-C & exit(), but what we are want to about discuss here is programmatically exiting.

Leave a Comment