Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ipaddr
✓ import ipaddr from 'ipaddr.js';
✗ import { ipaddr } from 'ipaddr.js';
The library exports a default object named `ipaddr` containing all methods and classes.
ipaddr (CommonJS)
✓ const ipaddr = require('ipaddr.js');
Standard CommonJS import pattern, compatible with older Node.js environments and bundlers.
IPv4, IPv6 (Types)
✓ import type { IPv4, IPv6, Address, Subnet } from 'ipaddr.js';
✗ import { IPv4, IPv6 } from 'ipaddr.js';
These are TypeScript type definitions, not runtime values. Use `import type` to avoid bundling issues.
Demonstrates parsing, validating, processing IPv4 and IPv6 addresses, CIDR matching, and identifying special IP ranges like loopback and private networks. Includes error handling for invalid inputs.
import ipaddr from 'ipaddr.js';
// --- Validation and Parsing ---
const ipAddressStringV4 = '192.168.1.100';
const ipAddressStringV6 = '2001:0db8:85a3::8a2e:0370:7334';
const invalidIp = '999.999.999.999';
const cidrV4 = '192.168.1.0/24';
const cidrV6 = '2001:db8::/32';
console.log(`Is '${ipAddressStringV4}' valid? ${ipaddr.isValid(ipAddressStringV4)}`);
console.log(`Is '${invalidIp}' valid? ${ipaddr.isValid(invalidIp)}`);
try {
const parsedV4 = ipaddr.parse(ipAddressStringV4);
console.log(`Parsed IPv4: ${parsedV4.toString()} (Kind: ${parsedV4.kind()})`);
const parsedV6 = ipaddr.parse(ipAddressStringV6);
console.log(`Parsed IPv6: ${parsedV6.toString()} (Kind: ${parsedV6.kind()})`);
// Demonstrate process() with an IPv4-mapped IPv6 address
const mappedV4inV6 = '::ffff:192.168.1.100';
const processedAddr = ipaddr.process(mappedV4inV6);
console.log(`Processed '${mappedV4inV6}': ${processedAddr.toString()} (Kind: ${processedAddr.kind()})`);
// --- CIDR Matching ---
console.log(`Is '${cidrV4}' a valid CIDR? ${ipaddr.isValidCIDR(cidrV4)}`);
const ipToMatchV4 = ipaddr.parse('192.168.1.50');
const [rangeV4, bitsV4] = ipaddr.parseCIDR(cidrV4);
console.log(`Does ${ipToMatchV4.toString()} match ${cidrV4}? ${ipToMatchV4.match(rangeV4, bitsV4)}`);
const ipToMatchV6 = ipaddr.parse('2001:db8:ffff::1');
const [rangeV6, bitsV6] = ipaddr.parseCIDR(cidrV6);
console.log(`Does ${ipToMatchV6.toString()} match ${cidrV6}? ${ipToMatchV6.match(rangeV6, bitsV6)}`);
// --- Special Ranges ---
const loopbackV4 = ipaddr.parse('127.0.0.1');
console.log(`'${loopbackV4.toString()}' is a '${loopbackV4.range()}' address.`);
const privateV4 = ipaddr.parse('10.0.0.1');
console.log(`'${privateV4.toString()}' is a '${privateV4.range()}' address.`);
} catch (error: any) {
console.error(`Error parsing IP address: ${error.message}`);
}
// Example of incorrect usage (type mismatch for match)
try {
const ipv4 = ipaddr.parse('192.168.1.1');
const ipv6Range = ipaddr.parse('2001:db8::');
// The .match method expects arguments of the same IP address family.
// Attempting this will not work as expected and might throw a runtime error.
// console.log(ipv4.match(ipv6Range, 16));
} catch (e: any) {
// This catch block would normally handle a TypeError if the match method was called with mismatched types.
// The actual method will silently return false or throw based on its internal logic, highlighting a gotcha.
// For demonstration, we'll just acknowledge the mismatch.
console.warn("Attempted to match IPv4 against IPv6 range. (Expected type mismatch, handled gracefully or via explicit check).");
}
Errors
Common errors & fixes
Error: Invalid IP address
Attempting to parse a malformed or syntactically incorrect IP address string using `ipaddr.parse()` without error handling.
fixValidate the input string with `ipaddr.isValid()` before parsing, or wrap `ipaddr.parse()` calls in a `try...catch` block to handle exceptions gracefully.
TypeError: ipaddr.isValid is not a function
Incorrect CommonJS `require` or ES module `import` syntax, leading to the `ipaddr` variable not correctly referencing the default export object.
fixFor CommonJS, use `const ipaddr = require('ipaddr.js');`. For ES modules, use `import ipaddr from 'ipaddr.js';`. Do not use named imports like `import { ipaddr } from 'ipaddr.js';`. TS2307: Cannot find module 'ipaddr.js' or its corresponding type declarations.
TypeScript compiler is unable to locate the type definition files for `ipaddr.js`. While `ipaddr.js` ships with its own types, this can occur in certain project configurations or if an older version is in use.
fixEnsure `ipaddr.js` is correctly installed. If the issue persists, verify your `tsconfig.json` paths or consider installing `@types/ipaddr.js` as a dev dependency (though typically not needed for recent versions of `ipaddr.js`).
Audit
Dependencies
No dependency data recorded yet.