{"id":10753,"library":"dnscache","title":"DNS Cache for Node.js","description":"dnscache is an unmaintained Node.js module (last published 7 years ago, current version 1.0.2) that transparently wraps the built-in `dns` module to provide an application-level caching layer for DNS lookup results. It operates on a GOF Proxy design pattern, intercepting all `require('dns')` calls to cache frequently accessed domain resolutions, aiming to reduce network latency and improve application performance. It offers configurable cache size and Time-To-Live (TTL) settings, and supports injecting a custom cache implementation. While it historically addressed Node.js's lack of built-in DNS caching, it is now largely superseded by more modern, actively maintained libraries and evolving Node.js DNS behaviors. There is no active release cadence, and it does not support modern ECMAScript Modules (ESM) syntax.","status":"abandoned","version":"1.0.2","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/yahoo/dnscache","tags":["javascript","dnscache","dns"],"install":[{"cmd":"npm install dnscache","lang":"bash","label":"npm"},{"cmd":"yarn add dnscache","lang":"bash","label":"yarn"},{"cmd":"pnpm add dnscache","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not support ESM imports.","wrong":"import dnscache from 'dnscache';","symbol":"dnscache","correct":"const dnscache = require('dnscache');"},{"note":"After initializing `dnscache`, subsequent `require('dns')` calls will also return the wrapped, cached DNS module.","wrong":"import * as dns from 'dns';","symbol":"dns","correct":"const dns = require('dns');"},{"note":"The module is configured by calling the imported `dnscache` function directly with an options object.","symbol":"Configuration options","correct":"dnscache({\n  \"enable\": true,\n  \"ttl\": 300,\n  \"cachesize\": 1000\n});"}],"quickstart":{"code":"const dns = require('dns');\nconst dnscache = require('dnscache')({\n    \"enable\": true,\n    \"ttl\": 300,\n    \"cachesize\": 1000\n});\n\nconsole.log('DNS Caching enabled with TTL:', dnscache.ttl, 'and cache size:', dnscache.cachesize);\n\n// Using the dnscache instance directly\ndnscache.lookup('www.yahoo.com', function(err, result) {\n    if (err) {\n        console.error('Yahoo lookup error:', err.message);\n        return;\n    }\n    console.log('Lookup www.yahoo.com (via dnscache.lookup):', result);\n});\n\n// Using the wrapped 'dns' module\ndns.lookup('www.google.com', function(err, result) {\n    if (err) {\n        console.error('Google lookup error:', err.message);\n        return;\n    }\n    console.log('Lookup www.google.com (via wrapped dns.lookup):', result);\n});\n\n// Example of a second lookup to demonstrate caching (though output won't show it directly)\nsetTimeout(() => {\n    dnscache.lookup('www.yahoo.com', function(err, result) {\n        if (err) {\n            console.error('Second Yahoo lookup error:', err.message);\n            return;\n        }\n        console.log('Second lookup www.yahoo.com (should be cached):', result);\n    });\n}, 500);\n\n// To prevent the process from exiting immediately for async operations\nsetTimeout(() => console.log('Exiting example.'), 2000);","lang":"javascript","description":"This quickstart demonstrates how to enable `dnscache` and use both the `dnscache` instance and the wrapped `dns` module for hostname lookups, showcasing the transparent caching mechanism."},"warnings":[{"fix":"Consider migrating to actively maintained DNS caching libraries like `cacheable-lookup` or application-level DNS resolution solutions (e.g., `tangerine`) that are compatible with modern Node.js versions and ESM, or leverage system-level DNS caching solutions.","message":"The `dnscache` package is abandoned, with its last publish date 7 years ago (April 22, 2019). It is not actively maintained, which poses security risks, compatibility issues with newer Node.js versions, and a lack of support for modern DNS features or practices.","severity":"breaking","affected_versions":">=1.0.2"},{"fix":"Always handle errors as the first argument in callback functions when using `dnscache` (e.g., `function(err, result) { if (err) { /* handle error */ } }`). Review your error handling logic for any code paths that might rely on synchronous `dns` throws.","message":"This module alters the error handling behavior of Node.js's built-in `dns` functions. While original `dns` functions might throw errors in certain situations, `dnscache` intercepts these and passes all errors as the first argument to the callback function, standardizing error propagation. Developers expecting synchronous throws might encounter unexpected behavior.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Carefully configure the `ttl` based on the expected volatility of your target domains' DNS records. Lower TTLs offer better consistency but might increase cache misses. Ensure proper monitoring for DNS resolution failures.","message":"Setting a high `ttl` (Time-To-Live) for cache entries can lead to stale DNS information if the IP addresses for a domain change. This can result in your application attempting to connect to an old, incorrect IP, leading to connection errors or service unavailability.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use CommonJS `require` syntax: `const dnscache = require('dnscache');`","cause":"Attempting to import `dnscache` using ECMAScript Modules (ESM) syntax (e.g., `import dnscache from 'dnscache'`).","error":"TypeError: dnscache is not a function"},{"fix":"Check network connectivity and DNS server configuration. If suspecting stale cache, reduce `ttl` or explicitly clear the cache if the library supports it (though `dnscache` does not expose a public cache clearing API, it relies on TTL expiration). The library configuration includes `ttl` and `cachesize` parameters.","cause":"While using `dnscache`, receiving ENOTFOUND errors, potentially due to DNS resolution issues or (less commonly) stale cache entries if a domain's IP changed but the cache was not updated.","error":"Error: getaddrinfo ENOTFOUND www.example.com"},{"fix":"Ensure `dnscache` methods are used with callbacks, e.g., `dnscache.lookup('host', (err, result) => { /* handle */ });`. If you need Promise-based DNS resolution, consider `node:dns/promises` or other modern alternatives.","cause":"`dnscache` is callback-based and does not return Promises. This warning indicates a misuse in an `async/await` context or an attempt to chain `.then()`/`.catch()` on a non-Promise return.","error":"UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()."}],"ecosystem":"npm"}