Say you are using Grunt or Gulp on a team. You need to have your Gulp or Gruntfile in version control so that any changes or improvements can be enjoyed (or endured) by everyone.

Now let’s say you all want to be running a cool task like Browser Sync and due to the network neck-beards and their crazy rules, you need to specify your host explicitly using your IP (eg. 192.168.X.XX). All your IP addresses are different though! Oh noes. Looks like you’ll have to manually edit it every time.

Or you could use the built in Node OS API. Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// REQUIRE THE OS MODULE
var os          = require('os');
var ifaces      = os.networkInterfaces();

/*...*/

// THIS FUNCTION GETS YOUR LOCAL IP SO WE CAN SERVE THE BUILT SITE FROM IT
// No more having to keep swapping the IP
var lookupIpAddress = null;
for (var dev in ifaces) {
    if(dev != "en1" && dev != "en0") {
        continue;
    }
    ifaces[dev].forEach(function(details){
        if (details.family=='IPv4') {
            lookupIpAddress = details.address;
        }
    });
};
var ipAddress   = lookupIpAddress;

/*...*/

// EXAMPLE ONLY - NOT REAL
browsersync: {
	options: {
		host: ipAddress, // Use the variable we stored earlier!
	}
}

Now whenever anyone runs the gulp or grunt (or any node thingy) it will get their IP and use that. Swag.

Full (Gulp) example as a gist

@matthewbeta