Categories
node.js

How to exit in Node.js

2154

What is the command that is used to exit? (i.e terminate the Node.js process)

1

2674

Call the global process object’s exit method:

process.exit()

From the docs:

process.exit([exitcode])

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

To exit with a ‘failure’ code:

process.exit(1);

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

7

  • 7

    Just want to add something. If you are handling a request, you should also end() the request as well. Otherwise, it’ll just hang.

    Mar 3, 2012 at 18:16

  • 136

    @pixelfreak, exit isn’t misleading at all. You are confused about how Node works. Think of Node as the server itself. It isn’t just fired up as needed, like PHP is within a web server like Apache. Node doesn’t even have to have anything to do with web servers at all! It’s just a host for some JavaScript, with some nifty built-in libraries for doing useful things.

    – Brad

    Sep 20, 2012 at 14:22

  • 6

    @Brad And PHP is a general purpose language. No need to run it with mod_php or use Apache. You can reimplement an httpd in PHP like node does if you really want or use a more sane/standardized approach like FastCGI just like you can in node.

    – binki

    Aug 8, 2016 at 1:30

  • 51

    Please note that process.exit() is not recommended, as described in this answer below.

    Dec 8, 2017 at 13:52

  • Off-topic slightly, but I’ve seen people use process.exit(-1). Why’s that?

    – PiggyPlex

    Jan 22, 2021 at 20:00

570

Just a note that using process.exit([number]) is not recommended practice.

Calling process.exit() will force the process to exit as quickly as
possible even if there are still asynchronous operations pending that
have not yet completed fully, including I/O operations to
process.stdout and process.stderr.

In most situations, it is not actually necessary to call
process.exit() explicitly. The Node.js process will exit on its own if
there is no additional work pending in the event loop. The
process.exitCode property can be set to tell the process which exit
code to use when the process exits gracefully.

For instance, the following example illustrates a misuse of the
process.exit() method that could lead to data printed to stdout being
truncated and lost:

// This is an example of what *not* to do:
if (someConditionNotMet()) {
  printUsageToStdout();
  process.exit(1);
}

The reason this is
problematic is because writes to process.stdout in Node.js are
sometimes asynchronous and may occur over multiple ticks of the
Node.js event loop. Calling process.exit(), however, forces the
process to exit before those additional writes to stdout can be
performed.

Rather than calling process.exit() directly, the code should set the
process.exitCode and allow the process to exit naturally by avoiding
scheduling any additional work for the event loop:

// How to properly set the exit code while letting
// the process exit gracefully.
if (someConditionNotMet()) {
  printUsageToStdout();  
  process.exitCode = 1;
}

19

  • 47

    This is the best answer, by far. Other answers might not allow node to properly process pending events before exit, very sad 🙁

    – djabraham

    Oct 26, 2016 at 5:13

  • 6

    Agreed, this should have more upvotes! I had a node script that was kicking off multiple child processes via shelljs.exec and I wanted my overall script to return with an error exit code if any of the child processes failed. process.exitCode = 1 worked great in the exec callbacks (whereas simply calling process.exit(1) in there would exit the main script before all child processes finished!)

    – Nick B

    Nov 23, 2016 at 15:10

  • 19

    I used this answer and found that my process never actually exited. Had to ctrl-C it.

    – jcollum

    Jan 26, 2017 at 16:32

  • 58

    There are many uses cases for immediate process and pending event termination. That is exactly what process.exit() is intended for.

    Jan 1, 2018 at 21:43

  • 17

    This is not the best answer by any stretch because it doesn’t answer the question. Instead, it gives best practices on how to develop code flow for a nodejs program.

    Jan 8, 2018 at 21:46


391

From the official nodejs.org documentation:

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);

8

  • 10

    @Alison yes, or more precisely code = 0; process.exit(code);

    – wprl

    Feb 21, 2013 at 16:29

  • 7

    Is it true that if you’re exiting, you probably don’t care about the value of code?

    – Armand

    Feb 21, 2013 at 17:26

  • 9

    @Alison A better idea is just process.exit() with no parameter as code defaults to 0

    Mar 28, 2014 at 18:01


  • 2

    @Armand You are correct — Code is just a variable, and in this case used to indicate what the parameter is. So .exit(0) does everything the example does.

    Jun 7, 2014 at 19:15

  • 26

    @Armand the code is not for you, it’s for whatever ran your code. For example, if you create an exit_0.js with process.exit(0); and run it with node exit_0.js && echo 'success' it will say “success”. If you create exit_1.js with process.exit(1); and run node exit_1.js && echo 'success' it will not say “success” since your process exited with a non-zero (which indicates a “failure” or “abnormal exit” to the shell). In addition, you will see different values in $? if you run node exit_1.js vs node exit_0.js (you can check by doing node exit_1.js and then doing echo $?).

    – msouth

    Aug 22, 2014 at 17:03