{

Different ways to exit Nodejs process| Process exit() method example


There multiple ways to exit a nodejs process with examples

NodeJS REPL Exit

REPL is an Node Command tool to execute node commands

Following is an command to exit NodeJS process

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

The above two ways works in Linux, Unix and Windows

For Mac, You can use Ctrl + D to exist

How to exit Nodejs Process using API programs?

In Programs, Nodejs exit process called in two ways

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

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

Here is an syntax.

process.exit([errorcode])

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

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

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

Nodejs process exit codes

Nodejs provides Process’s exit method has following error codes

ErrorCode   Description  
1 Uncaught fatal failures
2 Un reserved, can be used for bash
3 Internal Javascript parse error
4 Internal javascript evaludation failure
5 fatal error
128 Signal exits
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.