Install the ip package
mainYou can install the ip package via npm or by cloning the repository directly from GitHub.
npm
npm install ipgit
git clone https://github.com/indutny/node-ip.gitrepository·main·Indexed 23 days ago
https://github.com/indutny/node-ipA Node.js module providing utilities for retrieving, comparing, validating, and converting IP addresses. Version 2.0.1 includes support for IPv4 and IPv6 format validation, Buffer conversions, bitwise operations, and subnet information retrieval via CIDR or mask, including range checking with the .contains() method.
You can install the ip package via npm or by cloning the repository directly from GitHub.
npm install ipgit clone https://github.com/indutny/node-ip.gitThe ip module provides utilities for retrieving, comparing, validating, and converting IP addresses.
ip.address(): Returns your current IP address.ip.isEqual(addr1, addr2): Compares two IP addresses for equality.ip.isPrivate(addr): Checks if an IP address is private.ip.isV4Format(addr) / ip.isV6Format(addr): Validates the IP format.ip.toBuffer(addr): Converts an IP string to a Buffer.ip.toString(buffer): Converts a Buffer back to an IP string.ip.toLong(addr) / ip.fromLong(long): Converts between IPv4 strings and long integers.ip.mask(addr, mask): Returns the network address.ip.cidr(cidr): Returns the network address from a CIDR string.ip.not(mask): Returns the bitwise NOT of a mask.ip.or(addr, mask): Returns the bitwise OR of an address and a mask.You can operate on existing buffers using an offset to avoid extra allocations:
var buf = new Buffer(128);
var offset = 64;
ip.toBuffer('127.0.0.1', buf, offset); // [127, 0, 0, 1] at offset 64
ip.toString(buf, offset, 4); // '127.0.0.1'var ip = require('ip');
ip.address() // my ip address
ip.isEqual('::1', '::0:1'); // true
ip.toBuffer('127.0.0.1') // Buffer([127, 0, 0, 1])
ip.toString(new Buffer([127, 0, 0, 1])) // 127.0.0.1
ip.fromPrefixLen(24) // 255.255.255.0
ip.mask('192.168.1.134', '255.255.255.0') // 192.168.1.0
ip.cidr('192.168.1.134/26') // 192.168.1.128
ip.not('255.255.255.0') // 0.0.0.255
ip.or('192.168.1.134', '0.0.0.255') // 192.168.1.255
ip.isPrivate('127.0.0.1') // true
ip.isV4Format('127.0.0.1'); // true
ip.isV6Format('::ffff:127.0.0.1'); // true
// operate on buffers in-place
var buf = new Buffer(128);
var offset = 64;
ip.toBuffer('127.0.0.1', buf, offset); // [127, 0, 0, 1] at offset 64
ip.toString(buf, offset, 4); // '127.0.0.1'
// ipv4 long conversion
ip.toLong('127.0.0.1'); // 2130706433
ip.fromLong(2130706433); // '127.0.0.1'Use ip.subnet(address, mask) or ip.cidrSubnet(cidr) to retrieve detailed information about a network range. These methods return an object containing network details and a .contains(addr) method for range checking.
The returned object includes:
networkAddress: The starting address of the network.firstAddress: The first usable address.lastAddress: The last usable address.broadcastAddress: The broadcast address.subnetMask: The subnet mask string.subnetMaskLength: The prefix length (e.g., 26).numHosts: Number of available hosts.length: Total length of the subnet.contains: A function to check if an address belongs to this subnet.// Using CIDR notation
var subnet = ip.cidrSubnet('192.168.1.134/26');
console.log(subnet.networkAddress); // '192.168.1.128'
// Range checking
var isInRange = subnet.contains('192.168.1.190'); // trueip.subnet('192.168.1.134', '255.255.255.192')
// { networkAddress: '192.168.1.128',
// firstAddress: '192.168.1.129',
// lastAddress: '192.168.1.190',
// broadcastAddress: '192.168.1.191',
// subnetMask: '255.255.255.192',
// subnetMaskLength: 26,
// numHosts: 62,
// length: 64,
// contains: function(addr){...} }
ip.cidrSubnet('192.168.1.134/26')
// Same as previous.
// range checking
ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true