Skip to content

Instantly share code, notes, and snippets.

@syaau
Created August 24, 2017 07:47
Show Gist options
  • Save syaau/14c2f1bfa9a827eef1ede418a554c0e7 to your computer and use it in GitHub Desktop.
Save syaau/14c2f1bfa9a827eef1ede418a554c0e7 to your computer and use it in GitHub Desktop.
Retrieve the first Local IPv4 from the network interfaces
const os = require('os');
export default function getLocalIP() {
const ifaces = os.networkInterfaces();
const ifnames = Object.keys(ifaces);
for (let i = 0; i < ifnames.length; i += 1) {
const aliases = ifaces[ifnames[i]];
for (let j = 0; j < aliases.length; j += 1) {
const iface = aliases[j];
if (iface.family === 'IPv4' && !iface.internal) {
// Return the first ip address found
return iface.address;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment