Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 317 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exit in Node.js

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

#2
From the command line, `.exit` is what you want:

$ node
> .exit
$

It's documented in the [REPL docs][1]. REPL (Read-Eval-Print-Loop) is what the Node command line is called.

From a normal program, use [`process.exit([code])`][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
From code you can use `process.exit([errorcode])` where `[errorcode]` is an optional integer (`0` is the default to indicate success).

If you're using the [Read Eval Print Loop (REPL)][1], you can use <kbd>Ctrl</kbd> + <kbd>D</kbd>, or type `.exit`

Alternatively, on Windows or Linux you can use <kbd>Ctrl</kbd> + <kbd>C</kbd>, <kbd>Ctrl</kbd> + <kbd>C</kbd>

On Mac the command is <kbd>Ctrl</kbd> + <kbd>Z</kbd>, <kbd>Ctrl</kbd> + <kbd>Z</kbd>

[1]:

[To see links please register here]

Reply

#4
I have an application which I wanted to:

1. Send an email to the user
2. Exit with an error code

I had to hook `process.exit(code)` to an `exit` event handler, or else the mail will not be sent since calling `process.exit(code)` directly kills asynchronous events.

#!/usr/bin/nodejs
var mailer = require('nodemailer');
var transport = mailer.createTransport();
mail = {
to: 'Dave Bowman',
from: 'HAL 9000',
subject: 'Sorry Dave',
html: 'Im sorry, Dave. Im afraid I cant do <B>THAT</B>.'
}
transport.sendMail(mail);
//process.exit(1);
process.on('exit', function() { process.exit(1); });
Reply

#5
If you're in a Unix terminal or Windows command line and want to exit the Node REPL, either...

* Press <kbd>Ctrl</kbd> + <kbd>C</kbd> twice, or
* type `.exit` and press Enter, or
* press <kbd>Ctrl</kbd> + <kbd>D</kbd> at the start of a line (Unix only)

Reply

#6
From the official [nodejs.org](

[To see links please register here]

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

#7
As @Dominic pointed out, [throwing an uncaught error][1] is better practice instead of calling *process.exit([code])*:
`process.exitCode = 1;
throw new Error("my module xx condition failed");`




[1]:

[To see links please register here]

Reply

#8
To exit

let exitCode = 1;
process.exit(exitCode)

Useful exit codes
<pre>
1 - Catchall for general errors
2 - Misuse of shell builtins (according to Bash documentation)
126 - Command invoked cannot execute
127 - “command not found”
128 - Invalid argument to exit
128+n - Fatal error signal “n”
130 - Script terminated by Control-C
255\* - Exit status out of range
</pre>
Reply

#9
It depends on the reason why you're willing to exit node.js process, but in any case `process.exit()` is the **last option to consider**. A quote from documentation:

> It is important to note that 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 it's 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.

Let’s cover possible reasons why you might be willing to exit node.js process and why you should avoid `process.exit()`:


Case 1 - Execution complete (command line script)
---

If script has reached its end and node interpreter doesn't exit, it indicates that some async operations are still pending. It’s wrong to force process termination with `process.exit()` at this point. It’s better to try to understand [what is holding your script from exiting in expected way](

[To see links please register here]

). And when you settle this, you can use `process.exitCode` to return any result to calling process.


Case 2 - Termination because of external signal (SIGINT/SIGTERM/other)
---

For example, if you’re willing to gracefully shut down an **express** app. Unlike command line script, express app keeps running infinitely, waiting for new requests. `process.exit()` will be a bad option here because it’s going to interrupt all requests which are in pipeline. And some of them might be non-idempotent (UPDATE, DELETE). Client will never know if those requests are completed or not on server side and it might be the reason of data inconsistency between client and server. The only good solution is to tell http server to stop accepting new requests and wait for pending ones to finish with `server.close()`:

var express = require('express');
var app = express();
var server = app.listen(80);

process.on( 'SIGTERM', function () {
server.close(function () {
console.log("Finished all requests");
});
});


If it still doesn't exit - see Case 1.


Case 3 - Internal error
---

It's always better to `throw` an error, you’ll get a nicely formatted stack trace and error message. Upper levels of code can always decide if they can handle error (`catch`) or let it crash the process. On the other side, `process.exit(1)` will terminate process silently and there will be no chance to recover from this. It might be the only “benefit” of `process.exit()`, you can be sure that process will be terminated.


Reply

#10
**REPL**(Command Line)

- Press `ctrl + c` **twice**

- Type `.exit` and press enter

**Script File**

process.exit(code)

Node normally exits with **code 0** when no more async operations are pending.

`process.exit(1)` should be used to exit with a failure code.This will allow us to infer that node didn't close gracefully and was forced to close.

There are other exit codes like

3 - Internal JavaScript Parse Error ( very very rare)

5 - Fatal error in v8 javascript engine

9 - Invalid argument


For full list see [node exit codes][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through