Node: Improve error message for address already in use

Created on 18 Sep 2018  路  10Comments  路  Source: nodejs/node

Hello! This is my first issue on this repository, thanks for Node and all that y'all do!

In my class, sometimes students will start an Express server on the same port of one running in another terminal tab.

They get an error message like this:

15:20 $ node server.js 
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::5678
    at Server.setupListenHandle [as _listen2] (net.js:1336:14)
    at listenInCluster (net.js:1384:12)
    at Server.listen (net.js:1471:7)
    at Function.listen (/Users/ericlewis/Desktop/js-express-and-sequelize-lab/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/ericlewis/Desktop/js-express-and-sequelize-lab/server.js:11:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1363:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)

I wonder if we could improve this error message because it's a bit ambiguous what the actual problem is to newcomers.

I saw that this already came up in the Express issue tracker, where the suggestion was to change the error message in Node itself. The suggestion there was to add this context:

EADDRINUSE: http://localhost:3000 is already in use
errors net

All 10 comments

'::' is not localhost though, it's the ipv6 equivalent of '0.0.0.0'.

Also, I don't think putting protocol-specific information like 'http' in the error message is worthwhile. FWIW you can attach an 'error' event handler to your http server before calling listen() (explicitly or not) and get all of the information you need (errno/code, syscall, address, port) from the error object if you want to provide your own custom formatted message.

'::' is not localhost though, it's the ipv6 equivalent of '0.0.0.0'.
Also, I don't think putting protocol-specific information like 'http' in the error message is worthwhile.

Good point! What do you think of a more generalized message like

Cannot listen on :::3000. Another server on the local system is occupying that address.

Before changing the format of the message, we may need to deprecate/remove util._exceptionWithHostPort() first.

What is the reason for deprecate exceptionWithHostPort function inside the error.js file ?

@sagitsofan The error messages look like this because the error goes through exceptionWithHostPort so to change it you'll need to either modify that function (which is exported by accident) or keep the exported one and create a new internal function (introducing difference between the two).

IMO modifying or removing util._exceptionWithHostPort would be a breaking change.

On EADDRINUSE in particular: the human-readable error message defined by libuv is address already in use. If we are going to make the error message more readable, it would be easier to translate the code to what libuv defines (basically means doing something similar to what uvException in internal/errors does), than trying to rewrite human-readable error messages for every one of the 70+ types of errors.

Will send a new PR soon

@joyeecheung

ping

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filipesilvaa picture filipesilvaa  路  3Comments

akdor1154 picture akdor1154  路  3Comments

Brekmister picture Brekmister  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

willnwhite picture willnwhite  路  3Comments