Registry / http-networking / memoized-node-fetch

memoized-node-fetch

JSON →
library1.1.5jsnpmunverified

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-fetch
INSTALL
IMPORT
SIG · MEMOIZED-NODE-FETC
M
memoized-node-fetch
http-networkingjavascriptv1.1.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

memoizedNodeFetch
import memoizedNodeFetch from 'memoized-node-fetch';
import { memoizedNodeFetch } from 'memoized-node-fetch';
The primary factory function is exported as the default. Named imports for the factory function will fail.
memoizedNodeFetch (CommonJS)
const memoizedNodeFetch = require('memoized-node-fetch');
const { memoizedNodeFetch } = require('memoized-node-fetch');
For CommonJS, the default export is typically available directly on the module object. If using an older Node.js version or specific bundler configurations, '.default' might be explicitly required: `require('memoized-node-fetch').default`.
MemoizedFetchFunction (returned fetch)
import memoizedNodeFetch from 'memoized-node-fetch'; const fetch = memoizedNodeFetch(); // 'fetch' is now the memoized fetch function; its type is inferred as (url: RequestInfo, options?: RequestInit) => Promise<Response>.
import { fetch } from 'memoized-node-fetch';
The library exports a factory function, `memoizedNodeFetch`, which then returns the actual memoized fetch function. The memoized fetch function itself is not a direct named export from the package.

Demonstrates how concurrent identical requests return the same promise. It also clarifies that the cache is cleared once the promise resolves, preventing data caching.

import memoizedNodeFetch from 'memoized-node-fetch'; // Get a memoized fetch instance. This creates a new promise cache. const fetch = memoizedNodeFetch(); (async () => { console.log('Initiating two identical requests concurrently...'); const startTime = Date.now(); // Both calls to fetch for the same URL will return the same promise while it's in flight. const fetch1 = fetch('https://jsonplaceholder.typicode.com/todos/1'); const fetch2 = fetch('https://jsonplaceholder.typicode.com/todos/1'); // Verify that both calls indeed return the exact same promise object. console.log(`Are fetch1 and fetch2 the same promise object? ${fetch1 === fetch2}`); // Wait for the first (and only) actual network request to resolve. const res1 = await fetch1; // This will resolve immediately after fetch1, as it's the same promise. const res2 = await fetch2; const endTime = Date.now(); console.log(`Concurrent requests completed in ${endTime - startTime}ms`); // Log the JSON bodies, demonstrating the data consistency. const data1 = await res1.json(); const data2 = await res2.json(); console.log('Data from fetch1:', data1); console.log('Data from fetch2:', data2); console.log('\nDemonstrating non-caching after promise resolution...'); // After the promise resolves, it's removed from the cache. // A new request will result in a new promise. const fetch3 = fetch('https://jsonplaceholder.typicode.com/todos/1'); console.log(`Is fetch3 (after previous resolution) the same promise object as fetch1? ${fetch3 === fetch1}`); const res3 = await fetch3; const data3 = await res3.json(); console.log('Data from fetch3:', data3); })();
Debug
Known issues
gotchaThis library explicitly *only caches the promise* itself until it resolves or rejects. It does not store the resolved data. Therefore, any subsequent request for the same URL *after* the initial promise has settled will result in a brand new fetch call, not a retrieval of previously fetched data.
fix
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.
affects: >=1.0.0
gotchaThe caching key is generated by hashing the URL and JSON.stringifying the `RequestOptions`. This means objects within `RequestOptions` (e.g., `Headers` instances, functions, `Map`, `Set`, or plain objects with non-deterministic property order) might stringify inconsistently, leading to different keys and bypassing the cache for logically identical requests.
fix
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.
affects: >=1.0.0
gotchaThe package name `memoized-node-fetch` implies its primary use in Node.js environments with `node-fetch`. While it can wrap any `fetch`-like function, developers intending to use it in a browser environment *must* provide their own `fetch` implementation (e.g., the global `fetch`), as `node-fetch` is not browser-compatible and will not be automatically included.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: memoizedNodeFetch is not a function
This error typically occurs if `memoizedNodeFetch` is imported incorrectly (e.g., attempting a named import when it's a default export in ESM) or if the imported `memoizedNodeFetch` is not being called as a factory function to get the actual `fetch` instance.
fix
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();`
Expected cached data but got new request after initial response
Users often misunderstand this library as a data caching solution, expecting it to serve previously resolved data for subsequent requests, similar to `react-query` or `swr`.
fix
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.
ReferenceError: fetch is not defined
This error occurs in Node.js environments if `node-fetch` is not installed as a dependency and no custom `fetch`-like function is explicitly passed to `memoizedNodeFetch`.
fix
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);`
Upgrade
Version history
1.1.5latest on npm
Audit
Dependencies
node-fetchoptionalDefault fetch implementation if no custom fetch function is provided by the user.
Agent activity
2 hits · last 30 days
node
2
Resources
memoized-node-fetch — npm install memoized-node-fetch · libregistry