node-fetch-cache is a robust wrapper around the popular `node-fetch` library, providing an integrated caching layer for HTTP responses. It automatically caches responses from HTTP requests, serving subsequent identical requests directly from the cache without making a network call. The current stable version is 5.1.0, and the project appears to have an active release cadence with regular updates and maintenance, as indicated by recent releases adding new features like cache clearing. Key differentiators include its seamless integration with the standard `node-fetch` API, support for multiple cache backends (in-memory, file system, Redis via an adapter), and flexible control over caching behavior through `shouldCacheResponse` options. It's designed for Node.js environments, requiring Node.js 18.19.0 or higher, and ships with TypeScript types for improved developer experience.
npm install node-fetch-cacheVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `node-fetch-cache` with a file system cache, including setting a TTL, filtering responses to cache, and programmatically clearing the cache directory.
For in-memory caching with expiration, create a custom fetch instance: `NodeFetchCache.create({ cache: new MemoryCache({ ttl: 60000 }) })`. For persistent caching, consider `FileSystemCache` or `@node-fetch-cache/redis`.Implement a periodic cleanup mechanism by calling the `.clear()` method on your `FileSystemCache` instance, which will delete the entire cache directory. For example, `new FileSystemCache(options).clear()` in a cron job or scheduled task.
Consult the `node-fetch` documentation and release notes for relevant breaking changes when upgrading the underlying `node-fetch` version or `node-fetch-cache` itself.
Be explicit about where you define your caching options. If an option is present in both `create()` and `fetch()`, the `fetch()`-level option will override the `create()`-level option for that specific request.
Use ESM `import fetch from 'node-fetch-cache';` instead of `const fetch = require('node-fetch-cache');`.Ensure the `cacheDirectory` path is valid and accessible by the Node.js process. The library should create the directory if it doesn't exist, but permission issues or invalid root paths can prevent this. Check permissions and path correctness.