Installed NodeJS on Raspberry Pi, is there a way to check if the rPi is connected to the internet via NodeJs ?
10 Answers
A quick and dirty way is to check if Node can resolve www.google.com
:
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No connection");
} else {
console.log("Connected");
}
});
This isn't entire foolproof, since your RaspPi can be connected to the Internet yet unable to resolve www.google.com
for some reason, and you might also want to check err.type
to distinguish between 'unable to resolve' and 'cannot connect to a nameserver so the connection might be down').

- 5,207
- 5
- 39
- 55

- 198,204
- 35
- 394
- 381
-
1The solution depends upon google. Is there any alternate and something like a standard way? – Deep Kakkar Feb 23 '17 at 06:54
-
4@Deep if you have your own site, try to resolve it instead of google. Otherwise highly available servers like google are your best option. https://github.com/sindresorhus/is-online is an option – Ero Feb 24 '17 at 20:38
-
1It seems that this caches the result, so if your application lose connection after running this code it will give false impression that there is a connection. – BrunoLM May 11 '17 at 16:15
-
3for connection i would go for google dns servers, so you don't rely on your isp dns resolving google, which may or may not be blocked. li 8.8.8.8 – Caio Wilson Nov 16 '17 at 20:18
-
Doesn't work if machine already resolved google and the dns query is already cached in the machine – papar May 25 '18 at 09:36
-
what about with a port? like telnet? – Alexander Mills May 30 '19 at 23:22
While robertklep's solution works, it is far from being the best choice for this. It takes about 3 minutes for dns.resolve
to timeout and give an error if you don't have an internet connection, while dns.lookup
responds almost instantly with the error ENOTFOUND
.
So I made this function:
function checkInternet(cb) {
require('dns').lookup('google.com',function(err) {
if (err && err.code == "ENOTFOUND") {
cb(false);
} else {
cb(true);
}
})
}
// example usage:
checkInternet(function(isConnected) {
if (isConnected) {
// connected to the internet
} else {
// not connected to the internet
}
});
This is by far the fastest way of checking for internet connectivity and it avoids all errors that are not related to internet connectivity.

- 1,055
- 11
- 22
-
21Be careful with that solution! It's the one we choose to check the connectivity in our Electron app and it ends-up with a very bad suprise. `dns.lookup` runs on libuv's threadpool. So, for example, if you run the `checkInternet` through `setInterval` (i.e to monitor the connectivity), risks are that you may overflow the threadpool. **One of the consequence is blocking all the IO** in your app (since all fs operation also runs on libuv's threadpool). A good explanation [here](https://www.future-processing.pl/blog/on-problems-with-threads-in-node-js/) – Eturcim May 04 '18 at 13:51
-
5Unfortunately, this answer is incorrect. I guess there's DNS cache or something, that causes this solution to simply not work. It shows `true` even if I've disconnected from the internet. – Nurbol Alpysbayev Jul 25 '19 at 17:34
-
4dns.lookup() uses the operating system's DNS facilities which will cache the results, while dns.resolve() simply resolves the hostname directly. – Lamp Jan 31 '20 at 00:49
-
I had to build something similar in a NodeJS-app some time ago. The way I did it was to first use the networkInterfaces() function is the OS-module and then check if one or more interfaces have a non-internal IP.
If that was true, then I used exec() to start ping with a well-defined server (I like Google's DNS servers). By checking the return value of exec(), I know if ping was sucessful or not. I adjusted the number of pings based on the interface type. Forking a process introduces some overhead, but since this test is not performed too frequently in my app, I can afford it. Also, by using ping and IP-adresses, you dont depend on DNS being configured. Here is an example:
var exec = require('child_process').exec, child;
child = exec('ping -c 1 128.39.36.96', function(error, stdout, stderr){
if(error !== null)
console.log("Not available")
else
console.log("Available")
});

- 1,315
- 13
- 14
-
owwwh... this solutions is simple for a ping module i just discovered lately... :D Thanks @Kris – gumuruh Jul 04 '20 at 04:13
-
Sorry to say but this isn't foolproof as a simple Firewall Rule can disable ICMP Ping not only that, depending on your ISP (internet Service Provider) by default they can enable the fact of you not being able to externally ping probe other IP Addresses, EG: Three Mobile Network by DEFAULT has their Router Devices Firewall set to not being able to Ping Probe the IP, simple fix of disabling the ICMP Ping option on the Firewall, however there used to be whats known as ICMP Cripple (D)DoS Attack simply using ICMP Ping, however thanks to advance technology of 2022, we dont have that issue anymore.. – Johnty Oct 11 '22 at 16:57
It's not as foolproof as possible but get the job done:
var dns = require('dns');
dns.lookupService('8.8.8.8', 53, function(err, hostname, service){
console.log(hostname, service);
// google-public-dns-a.google.com domain
});
just use a simple if(err)
and treat the response adequately. :)
ps.: Please don't bother telling me 8.8.8.8 is not a name to be resolved, it's just a lookup for a highly available dns server from google. The intention is to check connectivity, not name resolution.

- 353
- 2
- 12
-
The solution depends upon google. Is there any alternate and something like a standard way? – Deep Kakkar Feb 23 '17 at 06:54
-
13There is no such "standard" way really because in order to test for **internet** connectivity you have to test something actually on the **internet**. I suppose you could check for anything you want, amazon, isp, cdn, IDK, just used google for the sake of simplicity, BTW using the IP makes it DNS independent. – Caio Wilson Mar 05 '17 at 10:30
-
-
Here is a one liner: (Node 10.6+)
let isConnected = !!await require('dns').promises.resolve('google.com').catch(()=>{});

- 19,976
- 6
- 58
- 55
Since I was concerned with DNS cache in other solutions here, I tried an actual connectivity test using http2. I think this is the best way to test the internet connection as it doesn't send much data and also doesn't rely on DNS resolving alone and it is quite fast.
Note that this was added in: v8.4.0
const http2 = require('http2');
function isConnected() {
return new Promise((resolve) => {
const client = http2.connect('https://www.google.com');
client.on('connect', () => {
resolve(true);
client.destroy();
});
client.on('error', () => {
resolve(false);
client.destroy();
});
});
};
isConnected().then(console.log);
Edit: I made this into a package if anyone is interested.

- 437
- 1
- 5
- 14
-
This works well, responding in 58ms according to my browser console (not a rigorous test). Seems the trick is it waits for a connection and not for the page it has requested to actually load, which improves the performance. Main reason I opted for this though, it overcomes the caching issues. I added an additional setting and function though to handle timeouts: `client.setTimeout(3000)` `client.on('timeout', () => {` – Maggie Aug 30 '22 at 11:15
As of 2019 you can use DNS promises lookup.
NOTE This API is experimental.
const dns = require('dns').promises;
exports.checkInternet = function checkInternet() {
return dns.lookup('google.com')
.then(() => true)
.catch(() => false);
};

- 998
- 11
- 18
I found a great and simple npm tool to detect internet connection. It's looks like more reliable.
First you need to install
npm i check-internet-connected
Then you can call it like follows
const checkInternetConnected = require('check-internet-connected');
const config = {
timeout: 5000, //timeout connecting to each server(A and AAAA), each try (default 5000)
retries: 5,//number of retries to do before failing (default 5)
domain: 'google.com'//the domain to check DNS record of
}
checkInternetConnected(config)
.then(() => {
console.log("Internet available");
}).catch((error) => {
console.log("No internet", error);
});

- 602
- 11
- 25
-
1This seems very over-complicated compared to the other answers (which it probably just wraps around) – Jun 20 '19 at 20:10
-
-
The other answers let you check with two lines (the dns lookup service), while this requires lots more code and requires an extra module which probably does just that. – Jun 25 '19 at 18:22
It is very helpful to check internet connection for our browser is available or not.
var internetAvailable = require("internet-available");
internetAvailable().then(function(){
console.log("Internet available",internetAvailable);
}).catch(function(){
console.log("No internet");
});
for more[internet-available][1]: https://www.npmjs.com/package/internet-available

- 921
- 10
- 11
It's a very simple function that does not import any stuff, but makes use of JS inbuilt function, and can only be executed when called, it does not monitor loss/internet connection; unlike some answers that make use of cache, this result is accurate as it does not return cached result.
const connected = fetch("https://google.com", {
method: "FET",
cache: "no-cache",
headers: { "Content-Type": "application/json" },
referrerPolicy: "no-referrer",
}).then(() => true)
.catch(() => false);
call it using await(make sure your'e inside an async function or you'll get a promise)
console.log(await connected);

- 6,687
- 5
- 44
- 67
-
The question is about checking network connection in NodeJs. But you've mentioned the API used in browsers only. – Neo Sep 20 '21 at 10:49