ipaddr.js allows you to verify and parse string representation of an IP address, match it against a CIDR range or range list, determine if it falls into some reserved ranges (examples include loopback and private ranges), and convert between IPv4 and IPv4-mapped IPv6 addresses.
npm install ipaddr.js
bower install ipaddr.js
ipaddr.js defines one object in the global scope:
ipaddr. In CommonJS,
it is exported from the module:
var ipaddr = ;
The API consists of several global methods and two classes: ipaddr.IPv6 and ipaddr.IPv4.
There are three global methods defined:
ipaddr.process. All of them receive a string as a single parameter.
ipaddr.isValid method returns
true if the address is a valid IPv4 or
IPv6 address, and
false otherwise. It does not throw any exceptions.
ipaddr.parse method returns an object representing the IP address,
or throws an
Error if the passed string is not a valid representation of an
ipaddr.process method works just like the
ipaddr.parse one, but it
automatically converts IPv4-mapped IPv6 addresses to their IPv4 counterparts
before returning. It is useful when you have a Node.js instance listening
on an IPv6 socket, and the
net.ivp6.bindv6only sysctl parameter (or its
equivalent on non-Linux OS) is set to 0. In this case, you can accept IPv4
connections on your IPv6-only socket, but the remote address will be mangled.
ipaddr.process method to automatically demangle it.
Parsing methods return an object which descends from
ipaddr.IPv4. These objects share some properties, but most of them differ.
One can determine the type of address by calling
addr.kind(). It will return
An address can be converted back to its string representation with
Note that this method:
- does not return the original string used to create the object (in fact, there is no way of getting that string)
- returns a compact representation (when it is applicable)
match(range, bits) method can be used to check if the address falls into a
certain CIDR range.
Note that an address can be (obviously) matched only against an address of the same type.
var addr = ipaddr;var range = ipaddr;addr; // => true
match can also be called as
match([range, bits]). In this way,
it can be used together with the
parseCIDR(string) method, which parses an IP
address together with a CIDR range.
var addr = ipaddr;addr; // => true
range() method returns one of predefined names for several special ranges defined
by IP protocols. The exact names (and their respective CIDR ranges) can be looked up
in the source: IPv6 ranges and IPv4 ranges. Some common ones include
(the default one) and
You can match against your own range list by using
ipaddr.subnetMatch(address, rangeList, defaultName) method. It can work with a mix of IPv6 or IPv4 addresses, and accepts a name-to-subnet map as the range list. For example:
var rangeList =documentationOnly: ipaddr 32tunnelProviders:ipaddr 32 // he.netipaddr 32 // freenet6;ipaddr; // => "tunnelProviders"
The addresses can be converted to their byte representation with
arrays of numbers, each in range of 0..255.)
var bytes = ipaddr; // ipv6.google.combytes // => [42, 0x00, 0x14, 0x50, 0x80, 0x07, 0x00, <zeroes...>, 0x00, 0x68 ]
ipaddr.IPv6 objects have some methods defined, too. All of them
have the same interface for both protocols, and are similar to global methods.
ipaddr.IPvX.isValid(string) can be used to check if the string is a valid address
for particular protocol, and
ipaddr.IPvX.parse(string) is the error-throwing parser.
ipaddr.IPvX.isValid(string) uses the same format for parsing as the POSIX
inet_ntoa function, which accepts unusual formats like
0x10000000. The function
ipaddr.IPv4.isValidFourPartDecimal(string) validates the IPv4 address and also ensures that it is written in four-part decimal format.
Sometimes you will want to convert IPv6 not to a compact string representation (with
:: substitution); the
toNormalizedString() method will return an address where
all zeroes are explicit.
var addr = ipaddr;addr; // => "2001:db8::1"addr; // => "2001:db8:0:0:0:0:0:1"
isIPv4MappedAddress() method will return
true if this address is an IPv4-mapped
toIPv4Address() will return an IPv4 object address.
To access the underlying binary representation of the address, use
var addr = ipaddr;addrparts // => [0x2001, 0xdb8, 0x10, 0, 0, 0, 0x1234, 0xdead]
A IPv6 zone index can be accessed via
var addr = ipaddr;addrzoneId // => 'eth0'
toIPv4MappedAddress() will return a corresponding IPv4-mapped IPv6 address.
To access the underlying representation of the address, use
var addr = ipaddr;addroctets // => [192, 168, 1, 1]
prefixLengthFromSubnetMask() will return a CIDR prefix length for a valid IPv4 netmask or
false if the netmask is not valid.
ipaddrIPv4 == 28ipaddrIPv4 == null
subnetMaskFromPrefixLength() will return an IPv4 netmask for a valid CIDR prefix length.
ipaddrIPv4 == "255.255.255.0"ipaddrIPv4 == "255.255.255.248"
broadcastAddressFromCIDR() will return the broadcast address for a given IPv4 interface and netmask in CIDR notation.
ipaddrIPv4 == "18.104.22.168"
networkAddressFromCIDR() will return the network address for a given IPv4 interface and netmask in CIDR notation.
ipaddrIPv4 == "22.214.171.124"
IPv4 and IPv6 can be converted bidirectionally to and from network byte order (MSB) byte arrays.
fromByteArray() method will take an array and create an appropriate IPv4 or IPv6 object
if the input satisfies the requirements. For IPv4 it has to be an array of four 8-bit values,
while for IPv6 it has to be an array of sixteen 8-bit values.
var addr = ipaddr;addr; // => "127.0.0.1"
var addr = ipaddraddr; // => "2001:db8::1"
Both objects also offer a
toByteArray() method, which returns an array in network byte order (MSB).
var addr = ipaddr;addr; // => [0x7f, 0, 0, 1]
var addr = ipaddr;addr; // => [0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]