Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent nodejs console close

Tags:

node.js

Is there any alternative to PHP error_reporting(0) in nodejs? I want to prevent app from closing after an error, I tried the try and catch method, but it doesn't work. So, how can I prevent node.js server from closing after an error ?


1 Answers

Edit: There is an event for uncaught errors:

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

See duplicate: Make node.js not exit on error

You could also use pm2 to run your script, pm2 will automatically restart your script on crash.

You need to catch the error. For example

try {
  error();
} catch(err) {
 // do nothing
}

There is no other way as far as i know. So you could fix or catch these errors only.

like image 126
Jannis Defu Avatar answered Aug 15 '26 09:08

Jannis Defu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!