Node: NodeJS defaults to IPv6 when listening for connections

Created on 8 Jan 2018  路  2Comments  路  Source: nodejs/node

  • NodeJS version: 6.12.2
  • Express version: 4.16.2
  • NPM version: 5.6.0
  • PM2 version: 2.9.1
  • Platform: Linux 3.10.0-693.11.1.el7.x86_64 #1 SMP Mon Dec 4 23:52:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: n/a


NodeJS recently began acting weirdly by defaulting to IPv6 (tcp6) on CentOS 7.x. I'm using ExpressJS and PM2 to run Node, and all firewalls were disabled.

Following are the results of the ss -tnlp and the netstat -tnlp commands when only app.listen(3000) is used:
screen shot 2018-01-08 at 8 06 51 pm
screen shot 2018-01-08 at 8 09 08 pm

Setting the host individually to app.listen(3000, "127.0.0.1") and app.listen(3000, "0.0.0.0") forces the app to use IPv4 however it refuses connections, as can be seen from the below screenshot:
screen shot 2018-01-08 at 8 14 08 pm

Following are the results when executing netstat -tnlp after forcing IPv4:
screen shot 2018-01-08 at 8 13 36 pm

question wrong repo

Most helpful comment

Listening on IPv6 took me by surprise as well, but apparently this behavior is described in the documentation:

If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
Note: In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the unspecified IPv4 address (0.0.0.0).

All 2 comments

It's listening on 127.0.0.1, a.k.a. localhost, not 0.0.0.0. Fix your code to use the right address and it should work.

I'm going to close this because this is not an issue with node.js itself. If you have follow-up questions, please post them to the help repo.

Listening on IPv6 took me by surprise as well, but apparently this behavior is described in the documentation:

If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
Note: In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the unspecified IPv4 address (0.0.0.0).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filipesilvaa picture filipesilvaa  路  3Comments

addaleax picture addaleax  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

Icemic picture Icemic  路  3Comments

mcollina picture mcollina  路  3Comments