Node.js server crashing without error message

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();
var errorHandler = require('errorhandler')

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

2019 update: you'll need to install the errorHandler package


In case anyone had a similar problem to me: I had a crashing Node.js server with no errors at all, and was pulling my hair out for an hour before realising that it was because in my code somewhere I was writing to a file like writeFileSync("./foo.json", "…"), which would of course normally be fine, but this was causing the server to "crash" because I was using PM2 to "watch" the server directory for file changes - i.e. whenever a file changes in the server directory, PM2 restarts the server. I solved this by adding a watch_ignore config for the .data folder and put foo.json in there.


Node v6.11.0 , Windows 10.

Tried the other suggestions here to no avail - app just stops, no error even using

process.on('uncaughtException',...)
process.on('unhandledRejection',....)

Finally tracked exit/crash down to a recursive function call. The following code demonstrates the problem ;

"use strict" ;

process.on('uncaughtException', function (exception) {
  console.log(exception); 
});

var count = 0 ;
function recursiveFunction(){
    console.log(count++);
    recursiveFunction();
}
recursiveFunction() ;

This will run so far then just stop. Try/Catch didn't work either - tried as above with ;

function recursiveFunction(){
    console.log(count++);
    try{
        recursiveFunction();
    }
    catch(e){
        console.log("recursion error");
    }
}

Again nothing - just stops.

A workaround (without having to redesign the code) is to use setImmediate (to avoid the recursion process);

function recursiveFunction(){
    console.log(count++);
    setImmediate(recursiveFunction);
}

(I eventually ctrl-c'd this to stop it.)

reported at node github issues


To complete @matteofigus answer, you can also listen for unhandled promise rejections.

process.on('unhandledRejection', (reason, p) => {
    console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
    // application specific logging, throwing an error, or other logic here
});

somePromise.then((res) => {
  return reportToUser(JSON.pasre(res)); // note the typo (`pasre`)
}); // no `.catch` or `.then`

Tags:

Node.Js