Repository navigation
Custom DNS lookup failing on Node 20+, works on Node 18 #55762
Copy link
Copy link
Closed
Description
Version
20.16.0
Platform
Microsoft Windows NT 10.0.22621.0
x64
Subsystem
No response
What steps will reproduce the bug?
const dns = require('dns');
const https = require('https');
// Custom DNS resolution for github.com
const customDNSLookup = (hostname, options, callback) => {
if (hostname === 'github.com') {
// Using one of GitHub's IPs as an example
return callback(null, '20.205.243.166', 4);
} else {
return dns.lookup(hostname, options, callback);
}
};
// Make a test request using native https
async function testConnection() {
const options = {
hostname: 'github.com',
path: '/',
method: 'GET',
headers: {
'Host': 'github.com'
},
agent: new https.Agent({
rejectUnauthorized: false,
lookup: customDNSLookup
})
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
console.log('Successfully connected to GitHub');
console.log('Using IP:', '20.205.243.166');
console.log('Status:', res.statusCode);
resolve(res);
});
req.on('error', (error) => {
console.error('Error:', error.message);
reject(error);
});
req.end();
});
}
testConnection();Just run this node repro.js
How often does it reproduce? Is there a required condition?
You can repro it on node v20+ it works on node v18
What is the expected behavior? Why is that the expected behavior?
Successfully connected to GitHub
Using IP: 20.205.243.166
Status: 200
This is the expected behavior because we have defined a custom lookup method to resolve to this IP
What do you see instead?
Error: Invalid IP address: undefined
node:internal/process/promises:391
triggerUncaughtException(err, true /* fromPromise */);
^
TypeError [ERR_INVALID_IP_ADDRESS]: Invalid IP address: undefined
at emitLookup (node:net:1465:17)
at customDNSLookup (C:\Aivan\PL\repro.js:8:16)
at emitLookup (node:net:1414:5)
at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
at lookupAndConnectMultiple (node:net:1413:3)
at node:net:1359:7
at defaultTriggerAsyncIdScope (node:internal/async_hooks:464:18)
at lookupAndConnect (node:net:1358:5)
at Socket.connect (node:net:1255:5)
at Object.connect (node:_tls_wrap:1801:13) {
code: 'ERR_INVALID_IP_ADDRESS'
}
Node.js v20.16.0
Additional information
I am testing on the same machine, same code above. Just different nodejs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
aduh95 commentedon Nov 7, 2024
Did you report this to axios? Can you reproduce without external deps?
geocine commentedon Nov 7, 2024
Yes I can repro I updated the code and removed axios
AlphaAzure-Bdeo commentedon Nov 7, 2024
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:
huseyinacacak-janea commentedon Nov 20, 2024
I’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.dario-colombo commentedon Jan 17, 2025
node <= 18
callback(err, address, family);
node >18
callback(err, [{address, family}]);
StefanStojanovic commentedon Feb 14, 2025
Closing this since based on the last few comments it seems to be explained. Feel free to reopen if needed.
Fix dns lookup callback, introduced by NodeJS 20
DongwonTTuna commentedon Jun 13, 2025
I fixed it by adding
familyoptions to Agent.