66

What is the PHP's equivalent of die()/exit() funciton in Node.js?

https://www.php.net/manual/en/function.exit.php

1
  • sometimes it is funny, when folks criticize php, while such untold primitive things (like exit and many other things) are not available in those "advanced" languages. in node.js unfotunately, you have to if/return from tens of functions correctly to exit . And moreover, how on earth, return (which should return different value) can be way for exit.. unbelievable. Commented Jun 19, 2020 at 17:38

6 Answers 6

83

process.exit() is the equivalent call.

Sign up to request clarification or add additional context in comments.

8 Comments

They are not equivalent. PHP's die() does not terminate the server as Node.js' process.exit() does, it merely stops terminating remaining code in the file. There is no equivalent, unfortunately.
@MMiller That is partially true because PHP has a totally different architecture. PHP has originated from CGI, in which case (i.e. single process executing the CGI script) it seems to be exactly equivalent to me as it stops the process. I consider FCGI and similar technologies merely as optimization than as gamechangers regarding this question. I therefore think your issues are quite nit-picky and not reflecting real-world problems IMO. Quite similarly, you could say that running multiple supervisord-started note instances behind a reverse proxy would make process.exit() not stop the server.
You can also use return if you are not in a function. There are some differences however. See my answer for more details. @MMiller I think this might be more what you mean?
@nl-x AFAIK using return outside of a function is pretty much equivalent to process.exit(), but he seems to be looking for a function that terminates the entire "server" whereas such things do not really exist for CGI-like applications such as PHP.
Since you also need to require() the library, a simple one-liner is require('process').exit();
|
29

I would use throw. Throw will cause the current request at hand to end, and will not terminate the node process. You can catch that output using your error view.

throw new Error('your die message here');

7 Comments

The advantage of throw compared to process.exit() is IMO that you have an error message. The disadvantage (or, sometimes, an advantage), is that it might be catched somewhere. Therefore I wouldn't consider it as a die() equivalent, but as an alternative for many usecases that one should consider.
Up-vote this! I'd much rather throw and get my catch in order instead of nuking the entire web server.
@AvadData That clearly depends on your use-case. The question is clearly for an equivalent to die(), whereas throw would be an equivalent to throw...
Smart, although there is also another limitation other than what @UliKöhler mentioned. throwing doesn't support expression usage, therefore you simply can't drop it in everywhere. Well, at least until this proposal lands into language github.com/tc39/proposal-throw-expressions
@hippietrail Yes, it will work within an async function. However, if the function is returning a promise, then the reject('your die message here') function should be called instead.
|
8

It needs to report to stderr (rather than stdout) and exit with a non-zero status to be die() ...

function die (errMsg) 
{
    if (errMsg)
        console.error(errMsg);
    process.exit(1);
}

Comments

5

If not in a function, you can use:

return;

But you can also use the suggestion of @UliKöhler:

process.exit();

There are some differences:

  • return ends more graceful. process.exit() more abrupt.
  • return does not set the exit code, like process.exit() does.

Example:

try {
    process.exitCode = 1;
    return 2;
}
finally {
    console.log('ending it...'); // this is shown
}

This will print ending it... on the console and exit with exit code 1.

try {
    process.exitCode = 1;
    process.exit(2);
}
finally {
    console.log('ending it...'); // this is not shown
}

This will print nothing on the console and exit with exit code 2.

Comments

0

You can now use the npm package dump-die. I took a look at the package on github and it practically uses process.exit(1).

Firstly, install it by npm install dump-die. It has a dd() function.

let foo = 'bar'
let hodor = { hodor: 'hodor' }
dd(foo, hodor)
// returns
// 'bar'
// { hodor: 'hodor' }

Comments

0

You can write:

return false After the block you want halt. For example:

if(something<=0)
    return false;

This will stop the operation. Because JS works on browser you can't stop the page process at certain point. This is re-operable. Use your imagination as requirement to use this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.