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

node.js

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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);

Solution 2 - node.js

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

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

Solution 3 - node.js

My version which was needed for a compact and single file script, hope to be useful for others:

var ifs = require('os').networkInterfaces();
var result = Object.keys(ifs)
  .map(x => [x, ifs[x].filter(x => x.family === 'IPv4')[0]])
  .filter(x => x[1])
  .map(x => x[1].address);

Or to answer the original question:

var ifs = require('os').networkInterfaces();
var result = Object.keys(ifs)
  .map(x => ifs[x].filter(x => x.family === 'IPv4' && !x.internal)[0])
  .filter(x => x)[0].address;

Solution 4 - node.js

$ npm install --save quick-local-ip

follwed by

var myip = require('quick-local-ip');

//getting ip4 network address of local system
myip.getLocalIP4();

//getting ip6 network address of local system
myip.getLocalIP6();

Solution 5 - node.js

https://github.com/dominictarr/my-local-ip

$ npm install -g my-local-ip
$ my-local-ip

or

$ npm install --save my-local-ip
$ node
> console.log(require('my-local-ip')())

A very small module that does just this.

Solution 6 - node.js

savage one-liner incoming

based on accepted answer, this one will build an array of objects with conditional entries depending on address properties

[{name: {interface name}, ip: {ip address}}, ...]
const ips = Object.entries(require("os").networkInterfaces()).reduce((acc, iface) => [...acc, ...(iface[1].reduce((acc, address) => acc || (address.family === "IPv4" && !address.internal), false) ? [{name: iface[0], ip: iface[1].filter(address => address.family === "IPv4" && !address.internal).map(address => address.address)[0]}] : [])], []);
console.log(ips);

Explained :

const ips = Object.entries(require("os").networkInterfaces()) // fetch network interfaces
.reduce((acc, iface) => [ // reduce to build output object
	...acc, // accumulator
	...(
		iface[1].reduce((acc, address) => acc || (address.family === "IPv4" && !address.internal), false) ? // conditional entry
		[ // validate, insert it in output
			{ // create {name, ip} object
				name: iface[0], // interface name
				ip: iface[1] // interface IP
				.filter(address => address.family === "IPv4" && !address.internal) // check is IPv4 && not internal
				.map(address => address.address)[0] // get IP
			}
		] 
		: 
		[] // ignore interface && ip
	)
], []);

Output example :

Array(4) [Object, Object, Object, Object]
length:4
__proto__:Array(0) [, …]
0:Object {name: "vEthernet (WSL)", ip: "172.31.xxx.x"}
1:Object {name: "Ethernet", ip: "10.0.x.xx"}
2:Object {name: "VMware Network Adapter VMnet1", ip: "192.168.xxx.x"}
3:Object {name: "VMware Network Adapter VMnet8", ip: "192.168.xx.x"}

Solution 7 - node.js

Ever since Node version 0.9.6 there is a simple way to get the server's IP address based on each request. This could be important if your machine has multiple IP addresses or even if you are doing something on localhost.

req.socket.localAddress will return the address of the machine node is running on based on the current connection.

If you have a public IP address of 1.2.3.4 and someone hits your node server from the outside then the value for req.socket.localAddress will be "1.2.3.4".

If you hit the same server from localhost then the address will be "127.0.0.1"

If your server has multiple public addresses then the value of req.socket.localAddress will be the correct address of the socket connection.

Solution 8 - node.js

Modified a bit Ebrahim's answer with some es6 and module syntax, for leaner code:

import { networkInterfaces } from "os";
const netInterfaces = networkInterfaces();
const [{ address }] = Object.values(netInterfaces).flatMap((netInterface) =>
  netInterface.filter((prop) => prop.family === "IPv4" && !prop.internal)
);
console.log(address) // -> '192.168...'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeltanovemberView Question on Stackoverflow
Solution 1 - node.jsseppo0010View Answer on Stackoverflow
Solution 2 - node.jsJan JůnaView Answer on Stackoverflow
Solution 3 - node.jsEbrahim ByagowiView Answer on Stackoverflow
Solution 4 - node.jsAlok G.View Answer on Stackoverflow
Solution 5 - node.jstheramView Answer on Stackoverflow
Solution 6 - node.jsnicopowaView Answer on Stackoverflow
Solution 7 - node.jsIntervaliaView Answer on Stackoverflow
Solution 8 - node.jsTzahi LehView Answer on Stackoverflow