Network Interface Utilities for Node.js

1.1.0 · abandoned · verified Tue Apr 21

The `network-interfaces` package provides synchronous utility functions for retrieving and filtering network interface and IP address information in Node.js. It acts as a wrapper around Node.js's built-in `os.networkInterfaces()` method, offering simplified functions like `toIp`, `toIps`, `fromIp`, `getInterface`, and `getInterfaces` with optional filtering by internal status and IP version (IPv4/IPv6). The current stable version is 1.1.0, last published in 2017. Due to its age and lack of maintenance, it is considered abandoned. Users are encouraged to directly use `os.networkInterfaces()` or seek more actively maintained alternatives for robust network interface management in modern Node.js applications, especially if TypeScript support or ESM compatibility is required.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `network-interfaces` to find external IPv4 interface names, retrieve an IP address for a given interface, and determine an interface name from a specific IP address, including error handling.

const ni = require('network-interfaces');

try {
  // Get all IPv4 external interface names
  const externalIpv4Interfaces = ni.getInterfaces({
    internal: false,
    ipVersion: 4
  });
  console.log('External IPv4 Interfaces:', externalIpv4Interfaces);

  if (externalIpv4Interfaces.length > 0) {
    const firstInterfaceName = externalIpv4Interfaces[0];
    // Get the first IPv4 address for a specific interface
    const firstIpv4 = ni.toIp(firstInterfaceName, { ipVersion: 4 });
    console.log(`First IPv4 address for ${firstInterfaceName}:`, firstIpv4);

    // Get the interface name from an IP address (e.g., localhost)
    const loopbackInterface = ni.fromIp('127.0.0.1');
    console.log('Interface for 127.0.0.1:', loopbackInterface);
  } else {
    console.log('No external IPv4 interfaces found.');
  }

} catch (error) {
  console.error('An error occurred:', error.message);
}

view raw JSON →