How to check internet connection status in NodeJs with example

Nodejs Is a javascript environment for Running applications on Desktop and IoT devices.

Sometimes, We need to check internet connection is available in IoT Rasberry PI devices.

There are multiple ways we can check the internet network connection in Nodejs.

How to detect the Internet connection is offline in Nodejs?

NodeJS has an inbuilt dns module for the domain name system(DNS) for IP address lookup and name resolution.

It provides lookup() function to check name resolution.

using the callback function, We can find whether the internet is online or offline.

Here is an example code to check internet connection status in NodeJS example

let dns = require('dns')

function isInternetOnline(callback) {
    dns.lookup('twitter.com', function (error) {
        if (error && error.code == "ENOTFOUND") {
        } else {

isInternetOnline(function (isOnline) {
    if (isOnline) {
        console.log("internet connection status is online");
    } else {
        console.log("internet connection status is offline");


It gives output below the output

the internet connection status is online
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.