Saber2pr's Blog

GetNativeIpIpv4

Use the built-in os module to access hardware device information.

const os = require("os")

module.exports = function getLocalIP() {
  const interfaces = os.networkInterfaces()
  for (const name of Object.keys(interfaces)) {
    for (const interf of interfaces[name]) {
      const { address, family, internal } = interf
      if (family === "IPv4" && !internal) {
        return address
      }
    }
  }
}