memoized-node-fetch is a JavaScript/TypeScript utility that wraps `node-fetch` (or any `fetch`-like function) to implement request promise memoization. Its primary function is to return the *same promise* for identical concurrent requests until that promise resolves, preventing redundant calls to external APIs. Unlike a traditional data cache, this library specifically caches the promise object itself, not the resolved data, and the cache is cleared immediately upon resolution or rejection. The current stable version is 1.1.5. While not strictly scheduled, the project appears to follow an as-needed release cadence for bug fixes and minor improvements. Its key differentiator from state management libraries like React Query or SWR is its focus on *deduplicating in-flight requests* rather than long-term data caching, making it complementary to such libraries when used as their underlying fetcher. It hashes the URL and request options to determine if requests are identical.
npm install memoized-node-fetchVerified import paths — ran on the pinned version, not inferred.
Demonstrates how concurrent identical requests return the same promise. It also clarifies that the cache is cleared once the promise resolves, preventing data caching.
Do not expect this library to function as a data cache. For persistent data caching (e.g., across page loads or component unmounts), consider integrating with dedicated data caching solutions like React Query, SWR, or a custom in-memory cache.
Ensure `RequestOptions` objects are consistently structured and primarily contain JSON-serializable primitives for reliable cache key generation. For complex options, you might need to normalize them before passing them to the fetch function, or accept that certain requests won't be deduplicated.
For browser usage, explicitly pass the global `fetch` function: `memoizedNodeFetch(globalThis.fetch)`. For Node.js, ensure `node-fetch` is installed (`npm install node-fetch`) if you are relying on the default implementation.
Ensure correct import syntax: `import memoizedNodeFetch from 'memoized-node-fetch';` (ESM) or `const memoizedNodeFetch = require('memoized-node-fetch');` (CJS). Then, remember to call it to get the memoized fetch instance: `const fetch = memoizedNodeFetch();`Understand that this library *only* caches the promise while it's in an in-flight state. Once the promise resolves or rejects, it's removed from the cache. For persistent data caching, integrate with a separate, dedicated data caching library.
To resolve this, install `node-fetch` (`npm install node-fetch`) which `memoized-node-fetch` will use by default if available. Alternatively, you can pass your own `fetch`-compatible function: `const fetch = memoizedNodeFetch(myCustomFetchFunction);`