Express: Cannot catch listen() addressinuse error....

Created on 18 Jan 2016  路  9Comments  路  Source: expressjs/express

I've tried everything i found online,

  • try-catch around listen()
  • listen().on('error', ...
  • process.on('uncaughtException ...

How do I catch the address in use error?? (express 4.x)

Most helpful comment

The "proper" way to do this is:

var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
server.on('error', function (e) {
  // do your thing
});
server.listen(3000);

Make the server yourself and bind the event before you start listening.

All 9 comments

Just wondering, why do you want to catch it? Isn't better if you "let it go"

I want to bunyan-log the error and then throw it

What version of Node.js are you using? With Express 4.13.3 on Node.js 0.12.1, I found your second option worked for me.

Simple test

var express = require('express');
var http = require('http');
var app = express();

http.createServer(function(req, res) {}).on('error', console.log).listen(3000);

// These should all print the error object to console
http.createServer(function(req, res) {}).on('error', console.log).listen(3000);
http.createServer(function(req, res) {}).listen(3000).on('error', console.log);
app.listen(3000).on('error', console.log);

Output I get

node test.js
{ [Error: listen EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen' }
{ [Error: listen EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen' }
{ [Error: listen EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen' }

The Server object is a Node.js EventEmitter. As with all EventEmitter's, _most_ errors are passed to the 'error' event. If no handler is registered for the 'error' event, those bubble up to be thrown. try/catch does not work because when listen is attempted, any errors that occur are caught and emitted on the 'error' event using process.nextTick() -- that is, by the time the error is actually reported, the try/catch block has already exited. As a fallback, you can register an 'uncaughtException' handler on process as a catch all for any unhandled errors that occur on any EventEmitter object, but it's best to simply set the 'error' callback on the server object. @tunniclm's example ought to work for you.

The "proper" way to do this is:

var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
server.on('error', function (e) {
  // do your thing
});
server.listen(3000);

Make the server yourself and bind the event before you start listening.

Hey guys,
it seems that running my server with "nodemon" causes different outcome than with "node".

  • with nodeamon index.js I get the exception thrown to the console, and my error handler is not called
  • with node index.js I get my handler called

anyone have any idea why?

I'm not that familiar with nodeamon but I would guess that it's likely hooking in it's own event listeners and taking actions before yours get a chance to fire.

I suspect that nodemon adds some error handler and handle the error for you (restart the process or wait for changes etc..). I took a quick look at the source code of nodemon, and found several places
For example https://github.com/remy/nodemon/blob/2f19ffb5a7162d08abbabea45ed995ac2cd9511a/lib/monitor/run.js#L101 and this https://github.com/remy/nodemon/blob/f5d1a04cc80f1bc963c20627177e545be42aeee7/lib/monitor/watch.js#L78

If you want to further investigate this issue, those two might contain some clues

Guys thanks for the help!

Was this page helpful?
0 / 5 - 0 ratings