Rick Eyre Rick Eyre - 1 year ago 57
Node.js Question

Node.js server crashing without error message

I have a Node.js server that is continually crashing without logging any kind of error message. Is this a typical scenario? How can I trap the error and log it before it crashes?

Answer Source

A good start would be to setup, especially in production, before setting the listener for your server, an handler for the exceptions that logs the details. Look at here:

process.on('uncaughtException', function (exception) {
  console.log(exception); // to see your exception details in the console
  // if you are on production, maybe you can send the exception details to your
  // email as well ?

If you are using Express.js, take a look at here to know how to see the full stack of your error (and eventually, again, send it to your email if you are on production). In that case, tell it to give you the full details before instantiating the listener:

var express = require('express');
// ...
var app = express();
// ...
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
// then, set the listener and do your stuff...