Repository navigation
"localhost" favours IPv6 in node v17, used to favour IPv4 #40537
Description
Activity
If it helps, I happened upon this is tests using Hapi. A Hapi Server listen address defaults to
'0.0.0.0'which pins it to IPv4. When my test made a client request usinglocalhostI saw the aboveECONNREFUSED ::1:$PORT.Reacted by Marc, Ivan Bovin and Adriel MartinsIt's #39987 -- Node.js now returns IP addresses in the order they are returned from the name resolver/DNS. There's a
--dns-result-ordercommand line option you can set to control the behaviour -- the difference in Node.js 17 is the default changed.Reacted by Bruno Ferreira, wanton7 and Christen Barringer- added a commit that references this issue
on Oct 20, 2021 Ah, which is the first "Other notable changes" in the release notes. 🤦 Thanks for the pointer! I'll close this, as it is definitely intended behaviour. A mention of this in some sort of "node v17 migration guide" would likely be helpful to others.
Reacted by Mike MacCana, Mike McCready, Ram Alvapillai, Vito Drofenik, Bruno Ferreira, JC (Jonathan Chen), Aaron Meese, rajmohan krishnamurthy, Adriel Martins and Andrej Leitner- added a commit that references this issue
on Nov 2, 2021 - added a commit that references this issue
on Jul 29, 2022 - added a commit that references this issue
on Aug 25, 2022 Just run into this.
one fix is to do this:
import dns from 'node:dns'; dns.setDefaultResultOrder('ipv4first');which reverts the old order
Reacted by Joe Becher, Wall-E 🤖, Saber Hosney, Dominique Quatravaux, Mike Pawlowski, Grant Kiely, Ilkka Järstä, Frumious Bandersnatch, Andrey Belym, Barry May and 3 more- added 3 commits that reference this issue
on Oct 12, 2022 17 remaining items
- added a commit that references this issue
on Oct 9, 2023 - added a commit that references this issue
on Nov 1, 2023 Here's my workaround (open-source and documented) that I hope that can help you too:
- privacy.sexy | force-ipv4: GitHub action to use for GitHub workflows.
- privacy.sexy |
force-ipv4.sh.: Script for general use.
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-ipv4and call it in GitHub runners. - Fixes the IPv6 request issues, and you can happily run e.g.
fetchAPI from Node.
Related commit introducing this fix: undergroundwires/privacy.sexy@52fadcd
Reacted by Chakrapani Gautam- added 2 commits that reference this issue
on Mar 30, 2024 - added a commit that references this issue
on Dec 6, 2024 - added a commit that references this issue
on Jan 27, 2026 - added a commit that references this issue
on Jan 27, 2026 - added a commit that references this issue
on Jan 27, 2026 - added a commit that references this issue
on Mar 10, 2026 - added a commit that references this issue
on Mar 10, 2026 - added 2 commits that reference this issue
on Sep 22, 2026 - added a commit that references this issue
on Oct 1, 2026
Version
v17.0.0
Platform
Darwin pink.local 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64
Subsystem
No response
What steps will reproduce the bug?
I'm not sure what the underlying mechanism is, but it looks to me like the resolution of "localhost" for
server.listen(PORT, HOST, ...)and forhttp.request('http://localhost/...', ...)changed from favouring IPv4 in node v16 and earlier, to favouring IPv6. The result is some possibly confusing breakages when using IPv4 values such as127.0.0.1and0.0.0.0. For example, the following script errors with node v17, but succeeds with node v16:Similarly, if we reverse things to listen on
'localhost'and GET from'http://[::1]:3000/ping'(IPv6), then it fails on node v16 and passes on node v17.As I said above, I don't know the mechanism for selecting IPv4 or IPv6. I suspect this isn't a "bug", except perhaps a lack of documentation? I don't see any mention of localhost of IPv6 in the changelog. Thanks.
How often does it reproduce? Is there a required condition?
Everytime. While I tested mostly on macOS, I believe I was seeing this in GitHub Action tests running on linux containers.
What is the expected behavior?
No response
What do you see instead?
No response
Additional information
No response