Skip to content

Instantly share code, notes, and snippets.

@BaileySimrell
Created January 16, 2022 22:41
Show Gist options
  • Save BaileySimrell/bd37c9e34f483f3e52409f28bebb3704 to your computer and use it in GitHub Desktop.
Save BaileySimrell/bd37c9e34f483f3e52409f28bebb3704 to your computer and use it in GitHub Desktop.
Retrieve local IP address with Node JS
'use strict';
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const results = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
console.log(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment