Skip to content

Instantly share code, notes, and snippets.

@bsorrentino
Created September 15, 2022 08:50
Show Gist options
  • Save bsorrentino/a629e78c1b81e9c8ccfd6870f76991ac to your computer and use it in GitHub Desktop.
Save bsorrentino/a629e78c1b81e9c8ccfd6870f76991ac to your computer and use it in GitHub Desktop.
get local ipv4 address in nodes
/**
* inspired by: https://github.com/IonicaBizau/local-ip-address
*
* @returns localIPv4 address
*/
function localIp4Address () {
const interfaces = Object.values(os.networkInterfaces())
for (let iface of interfaces) {
for (let alias of iface) {
if (alias.family === "IPv4"
&& alias.address !== "127.0.0.1"
&& !alias.internal) {
console.log( 'IPv4 address', alias.address)
return alias.address
}
}
}
throw new Error('IPv4 address not found!')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment