ERR_DNS_SET_SERVERS_FAILED
Node.jsERRORNotableDNSHIGH confidence

Failed to set DNS servers

Production Risk

DNS resolution will fail for all lookups if servers are set incorrectly.

What this means

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.

Why it happens
  1. 1Providing a hostname instead of an IP address for the DNS server
  2. 2Malformed IP address string in the servers array
  3. 3Invalid port number in the server address
How to reproduce

Triggered when setServers() validates the provided server addresses and finds one that is invalid.

trigger — this will error
trigger — this will error
const dns = require('dns');
dns.setServers(['invalid-hostname:53']); // hostname not allowed — must be IP

expected 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

Use IP addresses for DNS servers
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.

Code examples
Triggerjs
const dns = require('dns');
dns.setServers(['invalid-hostname:53']); // hostname not allowed — must be IP  // this triggers ERR_DNS_SET_SERVERS_FAILED
Handle in try/catchjs
try {
  // 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
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_dns_set_servers_failed(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Pass hostname strings to setServers()

setServers() requires numeric IP addresses; hostnames must be resolved before use.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors