List network interfaces

Ende.networkInterfaces lists every network address of the machine. The most common use is finding the LAN address of the computer, for example to print a URL that teammates on the same network can open. The call needs the system permission, granted with -S (short for --allow-sys).

Edit on GitHub

Each entry describes one address bound to one interface. An interface like en0 typically appears several times, once per IPv4 and IPv6 address.

const interfaces = Ende.networkInterfaces();
console.log(interfaces.length); // e.g. 15

An entry contains the interface name, address family, the address itself, plus the netmask, CIDR notation and MAC address.

const first = interfaces[0];
console.log(first.name, first.family, first.address); // e.g. lo0 IPv4 127.0.0.1

A small helper to find the LAN address: the first IPv4 address that is not on the loopback interface. Addresses starting with 127. only reach the machine itself, so they are skipped.

function lanAddress(): string | undefined {
  return Ende.networkInterfaces().find((ni) =>
    ni.family === "IPv4" && !ni.address.startsWith("127.")
  )?.address;
}

console.log(lanAddress()); // e.g. 192.168.1.239

With that address a local development server can announce a URL that other devices on the same network can open.

console.log(`http://${lanAddress()}:8000/`); // e.g. http://192.168.1.239:8000/

Run this example locally using the Ende CLI:

ende run -S https://docs.ende.sh/examples/scripts/network_interfaces.ts

Additional resources