DNS Module in Node.js (original) (raw)

Last Updated : 9 Mar, 2026

The Node.js DNS module provides methods for performing DNS lookups and working with domain names. It helps resolve domain names into IP addresses for network communication.

user

Features

The Node.js DNS module enables domain name resolution and DNS record queries.

**Example 1: Print the IP address of GeeksforGeeks on the console.

JavaScript `

// Include 'dns' module and create its object const dns = require('dns');

const website = 'geeksforgeeks.org'; // Call to lookup function of dns dns.lookup(website, (err, address, family) => { console.log('address of %s is %j family: IPv%s', website, address, family); });

// Execute using $ node ;

`

**Output:

address of geeksforgeeks.org is "52.25.109.230" family: IPv4

**Example 2: Print the IP address of GeeksforGeeks and perform a reverse DNS lookup to find the associated hostname.

JavaScript `

// Include 'dns' module and create its object const dns = require("dns");

// Call to reverse function along with lookup function. dns.lookup("www.geeksforgeeks.org", function onLookup(err, address, family) { console.log("address:", address); dns.reverse(address, function (err, hostnames) { console.log( "reverse for " + address + ": " + JSON.stringify(hostnames) ); }); });

`

**Output:

address: 52.222.176.140
reverse for 52.222.176.140: ["server-52-222-176-140.bom52.r.cloudfront.net"]

Benefits

The Node.js DNS module enhances network operations by simplifying domain resolution and efficiently managing DNS queries.

Also Check