Repository navigation
Custom DNS lookup failing on Node 20+, works on Node 18 #55762
Description
Activity
Did you report this to axios? Can you reproduce without external deps?
Did you report this to axios? Can you reproduce without external deps?
Yes I can repro I updated the code and removed axios
Had a similar issue. Apparently Node 20 expects a different output out of the function from Node18. Here is the current one that I have for Node 20 that works:
dns.lookup = function (domain: string, options: any, callback: any) { // Handle all possible function signatures if (typeof options === 'function') { callback = options; options = {}; } else if (typeof options === 'number') { options = { family: options }; } else if (!options) { options = {}; } // Check if we have any custom DNS entries const customEntry = domains.find(entry => { // Check if the domain matches exactly return domain === entry.source; }); if (customEntry) { if (options.all) { // Return array format when options.all is true callback(null, [{ address: customEntry.ip, family: customEntry.family || 4 }]); } else { // Return single address format callback(null, customEntry.ip, customEntry.family || 4); } return; } // Fall back to original DNS lookup if no custom entry found return dnsLookup.call(this, domain, options, callback); };Reacted by Aivan Monceller and Vytashuseyinacacak-janea commented
on Nov 20, 2024 ContributorMore actionsI’ve reviewed your issue and noticed that the default value of
autoSelectFamilyfor connections has changed starting from Node.js v20. In v20, the default value is nowtrue.If you want to run your code in Node.js v20 and maintain the previous behavior, you can use the
--no-network-family-autoselectionflag when starting Node.js. This will disable the automatic selection of the network family and might help address the issue you’re encountering.node <= 18
callback(err, address, family);
node >18
callback(err, [{address, family}]);Reacted by avcribl, Yst1214, dario colombo, Vytas, Chris Muiruri , Vladislav Bondar, IvanovRoman, Vaskire and ptorrentClosing this since based on the last few comments it seems to be explained. Feel free to reopen if needed.
- added a commit that references this issue
on Mar 26, 2025 I fixed it by adding
familyoptions to Agent.{ ..., dispatcher: new Agent( { connect: { lookup: _lookup, family: 4 // I added this } } ) }
Reacted by nodated and JamesFromIT
Version
20.16.0
Platform
Subsystem
No response
What steps will reproduce the bug?
Just run this
node repro.jsHow often does it reproduce? Is there a required condition?
You can repro it on node
v20+it works on nodev18What is the expected behavior? Why is that the expected behavior?
This is the expected behavior because we have defined a custom lookup method to resolve to this IP
What do you see instead?
Additional information
I am testing on the same machine, same code above. Just different nodejs