Get local IP address in node.js


I have a simple node.js program running on my machine and I want to get local IP address of PC on which is my program running. How do I get it with node.js?

7/12/2019 11:32:05 AM

Accepted Answer

'use strict';

var os = require('os');
var ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (ifname) {
  var alias = 0;

  ifaces[ifname].forEach(function (iface) {
    if ('IPv4' !== || iface.internal !== false) {
      // skip over internal (i.e. and non-ipv4 addresses

    if (alias >= 1) {
      // this single interface has multiple ipv4 addresses
      console.log(ifname + ':' + alias, iface.address);
    } else {
      // this interface has only one ipv4 adress
      console.log(ifname, iface.address);

// en0
// eth0
10/11/2015 6:12:45 PM

os.networkInterfaces as of right now doesn't work on windows. Running programs to parse the results seems a bit iffy. Here's what I use.

require('dns').lookup(require('os').hostname(), function (err, add, fam) {
  console.log('addr: '+add);

This should return your first network interface local ip.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow