nodeJs: dns.lookup error

This is my code

let dns = require('dns');
dns.lookup("", (err, add, fam) => {
    console.log("err:  " + err, "add: " + add, "fam: " + fam);

Why I am getting this in the console

err: Error: getaddrinfo EAI_AGAIN add: undefined fam: undefined

EAI_AGAIN is a DNS lookup timeout error… The Stack Overflow link below has an explanation and references to other links that may help:

Error: getaddrinfo EAI_AGAIN

You get add and fam as undefined because, since the lookup fails, they’re not set.

Also, a host name is the server name, not the URL, so you’d want to use


in the lookup

dns.lookup("", (err, add, fam) => {
    console.log("err:  " + err, "add: " + add, "fam: " + fam);

The node.js dns.lookup documentation is at dns.lookup(hostname[, options], callback)

You might also find Katacoda Node.js Playground useful. You can use it to try out snippets of code. You can try this snippet from the node.js documentation in it and see what should be returned:

const dns = require('dns');
const options = {
  family: 6,
  hints: dns.ADDRCONFIG | dns.V4MAPPED,
dns.lookup('', options, (err, address, family) =>
  console.log('address: %j family: IPv%s', address, family));

Be sure to change “” to a real host name like “”.

1 Like

But i want to check if a url is valid or not. I used this code.

let vaild = require('valid-url');


But this show yes for many urls that are not correct.

What do you mean by ‘not correct’ though, can you give an example?

this is a valid url according to that code 

It should be an invalid url as htt is not a protocol it’s http Or http

this is also a valid url

try changing the first line to match the valid.isUri… u have defined as vaild = require(…