Node.jsでローカルIPアドレスを取得する - Windows/macOS/Linux対応

問題

os.networkInterfaces()を使うとインターフェースが大量に表示されます。Windowsは「イーサネット」、macOSは「en0」、Linuxは「eth0」と、プラットフォームごとにコードを変える必要があります。

解決方法

const os = require('os');

function getLocalIP() {
  const interfaces = os.networkInterfaces();
  const platform = process.platform;

  const priority = {
    win32:  ['Ethernet', 'Wi-Fi'],
    darwin: ['en0', 'en1', 'en2'],
    linux:  ['eth0', 'eth1', 'wlan0'],
  };

  const names = priority[platform] || priority.linux;

  // 優先インターフェースから検索
  for (const name of names) {
    const iface = (interfaces[name] || [])
      .find(i => i.family === 'IPv4' && !i.internal);
    if (iface) return iface.address;
  }

  // 見つからない場合、仮想インターフェースを除外して検索
  for (const [name, addrs] of Object.entries(interfaces)) {
    if (/VMware|VirtualBox|Hyper-V/i.test(name)) continue;
    const iface = addrs.find(i => i.family === 'IPv4' && !i.internal);
    if (iface) return iface.address;
  }

  return null;
}

ポイント

  • macOSでは実際のネットワークはほぼen0(有線/Wi-Fi)です。Windowsではインターフェース名がローカライズされている場合があります。
  • VMwareやVirtualBoxなどの仮想インターフェースを除外しないと、実際のLAN IPの代わりに仮想ネットワークIP(172.x.x.x)が返される可能性があります。
  • iface.internaltrueの場合はループバック(127.0.0.1)なので、常に除外します。