You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to connect to establish a TCP connection (ECONNREFUSED) #40702
We have been having the same issue with node 17 and it appears to be a breaking change with how DNS.lookup works. With node 16, the lookup would return a IPv4 address but with node 17, it returns an IPv6 address which will break most REST clients that hardcode URLS like http://localhost:4040/api if the upstream server only binds to the IPv4 address (e.g. server.listen('127.0.0.1'...) etc..
sudo n latest
installed : v17.0.1 (with npm 8.1.0)
╭─[prompt redacted]›
╰─$ node
Welcome to Node.js v17.0.1.
Type ".help" for more information.
> require('dns').lookup('localhost', null, (e,a,f) => console.log(e,a,f))
GetAddrInfoReqWrap {
callback: [Function (anonymous)],
family: 0,
hostname: 'localhost',
oncomplete: [Function: onlookup]
}
> null ::1 6
.exitit
╭─[prompt redacted]›
╰─$ sudo n 16.2.0
installed : v16.2.0 (with npm 7.13.0)
╭─[prompt redacted]›
╰─$ node
Welcome to Node.js v16.2.0.
Type ".help" for more information.
> require('dns').lookup('localhost', null, (e,a,f) => console.log(e,a,f))
GetAddrInfoReqWrap {
callback: [Function (anonymous)],
family: 0,
hostname: 'localhost',
oncomplete: [Function: onlookup]
}
> null 127.0.0.1 4
Notice the results are null ::1 6 vs null 127.0.0.1 4 with null options
Reacted by Robert Morris, treysis, Max Schmitt, Muhammad Zaeem Nasir, Anatolii Smolianinov, Esteban C, Bruno Ferreira, lalalazero, Tim, 笑笑十年 and 2 more
For reference the change was #39987 -- Node.js no longer re-sorts results of IP address lookups and returns them as-is (i.e. it no longer ignores how your OS has been configured). You can change the behaviour via the verbatim option to dns.lookup() or set the --dns-result-order command line option to change the default.
Reacted by Sophie Bremer, Jakob Sachse, lili2012, Bruno Ferreira and Artem Zhivoderov
Thanks, @richardlau. How is one supposed to fix this if they are not the author of the server/client. Libraries like axios do not let you pass in the verbatim option so I suppose we all have to use the command line option until packages catch up.
Update to my comment above. When dealing with 3rd party libraries, we can use dns.setDefaultResultOrder('ipv4first') and have to be careful to use this in child_processes as well
Reacted by Remco Haszing, Filip Dupanović, Alexander Lyapko, Dbuggerx, Tommaso Lintrami, DevSleuth and joyceeee
@richardlau The better option would of course be to change localhost to 127.0.0.1 when connecting to legacy localhost.
@nsainaney If you cannot fix server/client, the solution would be to stay on the support NodeJS version, i.e. <17, until the authors of server/client add compatibility for Node v17.
Reacted by Narayan Sainaney, Miyuru, NUMAGA, Luispe, Hasan Badran, Jiyu Jang, shubhamk-sdei, Vikram Singh Panwar, Mario de Biase, Farghly Ahmed and 9 more
@nsainaney Yes, I'm just mentioning it because I consider it to be the cleanest way. If you set the setDefaultResultOrder option you might forget about that in the future and will limit IPv6 capability of your applications.
Reacted by Narayan Sainaney, DasSkelett, Miyuru and crayolito
Where can I find the OS configuration that Node.js now honors…
…on macOS?
…on Linux?
Sorry if it feels like a stupid question or like a question that belongs somewhere else, but I think more people than me don’t know this DNS stuff very well and will wonder why things “aren’t working”.
@mattdesl In your error message, you can see how there are two errors: Node tried both connecting via IPv4 127.0.0.1:5098, and via IPv6 ::1:5098, which is the Happy Eyeballs thing added in #44731.
I get that error too, because I have nothing running on port 5098. But when I start a server listening on 5098, OP’s code runs without errors:
require('http').createServer().listen(5098);// If you do this instead, the below code fails on Node.js 16 but works on later Node.js versions (unsure exactly which releases):// require('http').createServer().listen(5098, '::1');constconnect=(host,port)=>{socket=newrequire('net').Socket();socket.connect(port,host).on('connect',()=>console.log('konected!')).on('error',(e)=>console.log(e));};connect('localhost',5098);
After days of research and trial/error, this is how I got this working:
Create a script called force-ipv4.sh, that configures system to prefer IPv4 over IPv6, call it to configure the machine. It was not easy to find a reliable cross-platform solution and I went Cloudflare WARP for DNS resolution along with some system configurations.
To easily use the script in GitHub workflows, create GitHub action called force-ipv4 and call it in GitHub runners.
Fixes the IPv6 request issues, and you can happily run e.g. fetch API from Node.
Activity
nsainaney commentedon Nov 2, 2021
We have been having the same issue with node 17 and it appears to be a breaking change with how DNS.lookup works. With node 16, the lookup would return a IPv4 address but with node 17, it returns an IPv6 address which will break most REST clients that hardcode URLS like
http://localhost:4040/apiif the upstream server only binds to the IPv4 address (e.g.server.listen('127.0.0.1'...)etc..Notice the results are
null ::1 6vsnull 127.0.0.1 4with null optionsrichardlau commentedon Nov 2, 2021
For reference the change was #39987 -- Node.js no longer re-sorts results of IP address lookups and returns them as-is (i.e. it no longer ignores how your OS has been configured). You can change the behaviour via the
verbatimoption todns.lookup()or set the--dns-result-ordercommand line option to change the default.nsainaney commentedon Nov 2, 2021
Thanks, @richardlau. How is one supposed to fix this if they are not the author of the server/client. Libraries like
axiosdo not let you pass in theverbatimoption so I suppose we all have to use the command line option until packages catch up.nsainaney commentedon Nov 2, 2021
Update to my comment above. When dealing with 3rd party libraries, we can use dns.setDefaultResultOrder('ipv4first') and have to be careful to use this in child_processes as well
wibobm commentedon Nov 11, 2021
v17.0.1 and v17.1.0 have same issue with remote debugging
Error in debuggerConnector: connect ECONNREFUSED ::1:49629
Error: connect ECONNREFUSED ::1:49629
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
treysis commentedon Nov 12, 2021
@richardlau The better option would of course be to change
localhostto127.0.0.1when connecting to legacy localhost.@nsainaney If you cannot fix server/client, the solution would be to stay on the support NodeJS version, i.e. <17, until the authors of server/client add compatibility for Node v17.
nsainaney commentedon Nov 12, 2021
Thanks @treysis. There are enough workarounds that this isn't an issue for us.
treysis commentedon Nov 12, 2021
@nsainaney Yes, I'm just mentioning it because I consider it to be the cleanest way. If you set the
setDefaultResultOrderoption you might forget about that in the future and will limit IPv6 capability of your applications.nsainaney commentedon Nov 12, 2021
That's a good point. Thank you
Fix compatibility with Node.js 17 in `test/test.js`
Fix compatibility with Node.js 17 in `test/test.js`
lydell commentedon Apr 14, 2022
I run this:
Where can I find the OS configuration that Node.js now honors…
Sorry if it feels like a stupid question or like a question that belongs somewhere else, but I think more people than me don’t know this DNS stuff very well and will wonder why things “aren’t working”.
lexicalunit commentedon Apr 14, 2022
You have to change this to:
Which should work in both Node v16 and v17, as well as on macOS and Linux.
lydell commentedon Apr 14, 2022
@lexicalunit Yes, I read that in the various threads, but that’s not what I’m asking for.
69 remaining items
treysis commentedon Dec 19, 2023
Which error exactly and what do you do?
Rakeshbora007 commentedon Jan 4, 2024
NEXT_PUBLIC_API_URL=http://127.0.0.1:3000
it still not working formw
tahmidrahman-dsi commentedon Jan 19, 2024
Could anyone find this problem in Node v18.19.0 with Ubuntu 20.04.6?
treysis commentedon Jan 19, 2024
Which Node version? Did you try localhost instead of 127.0.0.1?
treysis commentedon Jan 19, 2024
Use Node 20!
1-Rishav commentedon Jan 27, 2024
Make sure that your password and port number of database should be correct because this "ERROR" take place when your database credentials are wrong.

mattdesl commentedon Feb 17, 2024
Is the code in the OP supposed to work on node 20?
I get the same error in node v20.11.1 and v21.6.2:
This also seems related to the error I'm getting from this demo app:
p2panda/mushroom-app-tutorial#7
lydell commentedon Feb 17, 2024
@mattdesl In your error message, you can see how there are two errors: Node tried both connecting via IPv4
127.0.0.1:5098, and via IPv6::1:5098, which is the Happy Eyeballs thing added in #44731.I get that error too, because I have nothing running on port 5098. But when I start a server listening on 5098, OP’s code runs without errors:
undergroundwires commentedon Mar 30, 2024
Here's my workaround (open-source and documented) that I hope that can help you too:
force-ipv4.sh.: Script for general use.After days of research and trial/error, this is how I got this working:
force-ipv4.sh, that configures system to prefer IPv4 over IPv6, call it to configure the machine. It was not easy to find a reliable cross-platform solution and I went Cloudflare WARP for DNS resolution along with some system configurations.force-ipv4and call it in GitHub runners.fetchAPI from Node.Related commit introducing this fix: undergroundwires/privacy.sexy@52fadcd
ci/cd: bump Node.js environment to 20.x
ci/cd: bump Node.js environment to 20.x
Make tests run in Node >=17. Wow…
🐳 Fix `ECONNREFUSED ::1:3000` error
🐳 Fix `ECONNREFUSED ::1:3000` error
🐳 Fix `ECONNREFUSED ::1:3000` error