Registry / devops / dns-cache

dns-cache

JSON →
library2.0.0jsnpmunverified

Simple DNS cache for Node.js that automatically caches all dns.lookup calls with configurable TTL. Current stable version is 2.0.0, compatible with Node.js >=0.12.0. Version 2.0.0 is not backwards compatible with Node.js <0.12 due to changes in dns.lookup. For Node.js 0.10 and older, use version 1.0.0. This package addresses DNS lookup conflicts (see joyent/node#7729) and improves performance by reducing redundant DNS queries. It is lightweight, easy to use (just require once), and accepts a TTL in milliseconds.

npm install dns-cache
INSTALL
IMPORT
SIG · DNS-CACHE
D
dns-cache
devopsjavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import dnscache from 'dns-cache';
const { default: dnscache } = require('dns-cache');
ESM import; the package exports a function that must be called to enable caching.
default
const dnscache = require('dns-cache');
const { dnscache } = require('dns-cache');
CJS require; returns the function directly, not an object with a named export.
none
require('dns-cache')(10000);
const cache = require('dns-cache'); cache(10000);
Most users just require and immediately invoke; storing the result is optional.

Enables DNS caching with a 10-second TTL and performs a lookup.

const dnscache = require('dns-cache')(10000); // All subsequent dns.lookup calls are cached for 10 seconds. require('dns').lookup('example.com', (err, address, family) => { console.log(address); // cached result });
Debug
Known issues
breakingVersion 2.0.0 is not backwards compatible with Node.js < 0.12
fix
Use version 1.0.0 for Node.js 0.10 and older.
affects: 2.0.0
breakingChanges to dns.lookup in Node.js 0.12 break version 1.x compatibility
fix
Upgrade to version 2.0.0 if using Node.js 0.12 or newer.
affects: >=0.12
gotchaCache size not limited; can grow unbounded with many unique hostnames
fix
Monitor cache size or implement custom pruning if necessary.
affects: >=1.0.0
gotchaDNS cache is global and affects all dns.lookup calls in the process
fix
Consider using scoped lookups or per-module caching if isolation is needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: require(...) is not a function
Importing without calling the returned function
fix
const dnscache = require('dns-cache')(ttl);
dns-cache: TTL must be a number
Passing non-numeric TTL
fix
Call require('dns-cache')(1000) with a numeric TTL in milliseconds.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
dns-cache — npm install dns-cache · libregistry