Learn how to exit process in Nodejs

There are multiple ways to exit a nodejs process with examples

NodeJS REPL Exit

REPL is a Node Command tool to execute node commands

Following is a command to exit the NodeJS process

  • First way, Select Ctrl + C two times
  • Another way using type .exit and click enter

The above two ways work in Linux, Unix, and Windows

For Mac, You can use Ctrl + D to exist

How to exit Nodejs Process using API programs?

In Programs, the Nodejs exit process called in two ways

one way by calling process.exit() manually. Another way is, It calls automatically when there are no pending tasks to process by the event loop.

Nodejs contains a global process object that contains the exit() method.

Here is a syntax.

process.exit([errorcode])

code is an optional integer value, default is zero, which means success. Code=1 is a failure

For success, call process.exit(0) method. This executes normally when no asynchronous operations are pending. For failure, call process.exit(1) method.

Since the process is an instance of EventEmitter, the calling process.exit() method exhibits the exit event, which terminates all work including asynchronous operations still in progress.

Nodejs process exit codes

Nodejs provides Process’s exit method has the following error codes

ErrorCodeDescription
1Uncaught fatal failures
2Unreserved, can be used for bash
3Internal Javascript parse error
4Internal javascript evaluation failure
5fatal error
128Signal exits