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?
-
1In addition to logging uncaught exceptions to find the cause of the crash, you may check to see if the process is running out of memory. – Phillip Kinkade Nov 09 '13 at 21:34
-
1Can you recommend a way to check the memory usage of the server? – Rick Eyre Nov 11 '13 at 16:28
-
The way I did it two years ago was to run my node server from a shell and capture stderr. V8 at the time would unceremoniously exit and write an error message out to stderr. However, V8 has changed a lot, and I don't know if that technique would work any more. Monitoring process memory may help if the crash happens after a long time. – Phillip Kinkade Nov 11 '13 at 18:13
5 Answers
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

- 960
- 9
- 11
-
9`errorHandler` middleware is no longer bundled with express. You may install it by rinning `npm install errorhandler` — see details here https://github.com/expressjs/errorhandler – muchweb Apr 21 '15 at 10:26
-
2It turns out that SQL was silently crashing the process and I didn't know. so thanks to this I was able to track that down. Thanks – J-Cake Sep 30 '19 at 04:27
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`

- 3,954
- 26
- 34
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.

- 3,752
- 1
- 32
- 41
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

- 1,589
- 17
- 25
You can use middleware called 'errorhandler' & with 'logger' (log4js-npm-package) you can keep log for all error exceptions. Here's the code for Errorhandler :
// Middleware: Catch-All Error Handler. So that we log errors, but don't leak internal error details to the client.
app.use(errorHandler);
function errorHandler(err, req, res, next) {
// XHR Request?
if (req.xhr) {
logger.error(err);
res.status(500).send({ error: 'Internal Error Occured.' });
return;
}
// Not a XHR Request.
logger.error(err);
res.status(500);
res.render('framework/error', { error: "Internal Server Error." });
// Note: No need to call next() as the buck stops here.
return;
}

- 198
- 2
- 12