Registry / devops / fetch-cached-dns

fetch-cached-dns

JSON →
library2.0.5jsnpmunverified

Decorator for fetch that caches DNS resolutions per hostname, reducing DNS lookup overhead on repeated requests to the same host. Version 2.0.5 is the latest stable release; the project is a fork of @vercel/fetch-cached-dns with ongoing maintenance. Unlike other fetch wrappers, it transparently integrates with any fetch-compatible library (defaults to node-fetch) and handles redirects with an onRedirect callback. Suitable for high-throughput Node.js services where DNS latency is a bottleneck.

npm install fetch-cached-dns
INSTALL
IMPORT
SIG · FETCH-CACHED-DNS
F
fetch-cached-dns
devopsjavascriptv2.0.5
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
const fetch = require('fetch-cached-dns')(require('node-fetch'))
import fetch from 'fetch-cached-dns'
Package exports a factory function, not a fetch instance. CommonJS only; ESM not supported.
default
const fetch = require('fetch-cached-dns')()
const fetch = require('fetch-cached-dns')
Call the export with optional fetch implementation (defaults to node-fetch). Without calling, you get the factory function.
default
const fetch = require('fetch-cached-dns')(customFetch)
Pass any fetch-compatible function (e.g., undici's fetch) as argument.

Creates a fetch with cached DNS resolution and handles redirects via onRedirect callback.

const nodeFetch = require('node-fetch'); const fetch = require('fetch-cached-dns')(nodeFetch); async function main() { try { const response = await fetch('https://example.com', { onRedirect: (resp, opts) => { console.log('Redirected to:', opts.url); } }); const text = await response.text(); console.log(text.substring(0, 100)); } catch (err) { console.error('Fetch failed:', err); } } main();
Debug
Known issues
gotchaThe package expects a CommonJS environment; do not use ES module imports.
fix
Use require() instead of import. If you must use ESM, wrap with createRequire from 'module'.
affects: >=0.0.0
gotchaIf node-fetch is not installed, calling fetch() will throw a missing peer dependency error.
fix
Install node-fetch as a dependency, or pass an alternative fetch implementation.
affects: >=0.0.0
gotchaDNS cache is in-memory and does not respect TTL from DNS records; manual cache eviction not exposed.
fix
No workaround; consider using a dedicated DNS caching library if TTL compliance is needed.
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'node-fetch'
Missing peer dependency node-fetch
fix
npm install node-fetch
fetchCachedDns is not a function
Attempted to import default export as a fetch instance instead of calling factory
fix
Use require('fetch-cached-dns')(require('node-fetch')) instead of just require('fetch-cached-dns')
Upgrade
Version history
2.0.5latest on npm
Audit
Dependencies
node-fetchoptionalpeer dependency used as default fetch implementation if not supplied
Agent activity
2 hits · last 30 days
node
2
Resources
fetch-cached-dns — npm install fetch-cached-dns · libregistry