Failed to set DNS servers
Production Risk
DNS resolution will fail for all lookups if servers are set incorrectly.
Thrown when dns.setServers() or resolver.setServers() fails because one or more of the provided server addresses is invalid or in an unsupported format. Each server must be a valid IP address with an optional port.
- 1Providing a hostname instead of an IP address for the DNS server
- 2Malformed IP address string in the servers array
- 3Invalid port number in the server address
Triggered when setServers() validates the provided server addresses and finds one that is invalid.
const dns = require('dns');
dns.setServers(['invalid-hostname:53']); // hostname not allowed — must be IPexpected output
Error [ERR_DNS_SET_SERVERS_FAILED]: c-ares failed to set servers: "invalid-hostname:53" is not a valid address
Fix
Use IP addresses for DNS servers
WHEN Always — setServers() requires IP addresses, not hostnames
const dns = require('dns');
dns.setServers([
'8.8.8.8', // Google DNS
'1.1.1.1', // Cloudflare DNS
'[2001:4860:4860::8888]', // IPv6 with brackets
]);Why this works
c-ares (the DNS library) requires numeric IP addresses for server configuration.
const dns = require('dns');
dns.setServers(['invalid-hostname:53']); // hostname not allowed — must be IP // this triggers ERR_DNS_SET_SERVERS_FAILEDtry {
// operation that may throw ERR_DNS_SET_SERVERS_FAILED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_DNS_SET_SERVERS_FAILED') {
console.error('ERR_DNS_SET_SERVERS_FAILED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_dns_set_servers_failed(...args) {
// validate args here
return performOperation(...args)
}✕ Pass hostname strings to setServers()
setServers() requires numeric IP addresses; hostnames must be resolved before use.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev