Created
September 15, 2022 08:50
-
-
Save bsorrentino/a629e78c1b81e9c8ccfd6870f76991ac to your computer and use it in GitHub Desktop.
get local ipv4 address in nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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