How can I get the local IP address in Node.js?


Question

I'm not referring to

127.0.0.1

But rather the one that other computers would use to access the machine e.g.

192.168.1.6

1
61
5/25/2012 7:31:26 AM

Accepted Answer

http://nodejs.org/api/os.html#os_os_networkinterfaces

var os = require('os');

var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
        var address = interfaces[k][k2];
        if (address.family === 'IPv4' && !address.internal) {
            addresses.push(address.address);
        }
    }
}

console.log(addresses);
112
9/20/2014 9:26:34 AM

https://github.com/indutny/node-ip

var ip = require("ip");
console.dir ( ip.address() );

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