Registry / devops / fetch-retry

fetch-retry

JSON →
library6.0.0jsnpmunverified

Adds retry functionality to any fetch library (isomorphic-fetch, cross-fetch, unfetch, or Node.js native fetch). v6.0.0 wraps an existing fetch implementation and retries failed requests due to network errors or specific HTTP status codes. Defaults to 3 retries with 1000ms delay. Supports exponential backoff via custom retryDelay function. Ships TypeScript types. Lightweight with no additional dependencies beyond a fetch polyfill. Compatible with browser and Node.js (>=18).

npm install fetch-retry
INSTALL
IMPORT
SIG · FETCH-RETRY
F
fetch-retry
devopsjavascriptv6.0.0
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.

fetch-retry (default export)
import fetchRetry from 'fetch-retry'; const fetch = fetchRetry(global.fetch);
import { fetchRetry } from 'fetch-retry'; // wrong: no named export
fetch-retry exports a function that wraps a fetch implementation. Use default import in ESM.
fetch-retry with CommonJS
const fetchRetry = require('fetch-retry'); const fetch = fetchRetry(require('isomorphic-fetch'));
const fetch = require('fetch-retry')(require('isomorphic-fetch')); // wrong: require returns a function, not a fetch instance
In CJS, require('fetch-retry') returns the wrapper function, not a fetch instance.
fetch-retry with TypeScript
import fetchRetry from 'fetch-retry'; const fetch = fetchRetry(fetch as any); // or use a typed fetch implementation
import fetchRetry = require('fetch-retry'); // wrong: uses ES import syntax for CJS module
TypeScript types are included. The wrapped fetch may need type assertion if using native fetch.
fetch-retry with Node.js native fetch
import fetchRetry from 'fetch-retry'; const fetch = fetchRetry(globalThis.fetch);
const fetch = require('fetch-retry')(global.fetch); // wrong: global.fetch may not exist in older Node versions
Node.js 18+ has native fetch on globalThis. For older versions, use a polyfill like cross-fetch.

Shows wrapping cross-fetch with fetch-retry, custom exponential backoff delay, and retrying on 503 status codes.

import fetchRetry from 'fetch-retry'; import fetch from 'cross-fetch'; const fetchWithRetry = fetchRetry(fetch); const url = process.env.API_URL ?? 'https://api.example.com/data'; const apiKey = process.env.API_KEY ?? ''; fetchWithRetry(url, { headers: { 'Authorization': `Bearer ${apiKey}` }, retries: 3, retryDelay: (attempt) => Math.pow(2, attempt) * 1000, retryOn: [503] }) .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.error(err));
Debug
Known issues
breakingfetch-retry no longer exports a pre-bound fetch; you must pass a fetch implementation to the exported function.
fix
Use fetchRetry(originalFetch) instead of require('fetch-retry')(originalFetch) which is now required.
affects: >=5.0.0
deprecatedfetch-retry v5 dropped support for Node.js < 10; v6 drops support for Node.js < 12.
fix
Upgrade to Node.js >= 12 or use an older version of fetch-retry (but note security risks).
affects: >=6.0.0
gotchafetch-retry does not handle request body streaming; if the original fetch implementation streams the body, retry may fail.
fix
Ensure the request body is not a stream or clone it before retrying. Alternatively, use a custom retry function to recreate the request.
affects: >=1.0.0
gotchaIf using fetch-retry with isomorphic-fetch, note that isomorphic-fetch adds a global fetch which may conflict.
fix
Use cross-fetch or node-fetch instead, or ensure you wrap the correct fetch instance.
affects: >=1.0.0
breakingIn v5, the default export is a function that must be called with a fetch implementation. Previously it was a direct wrapped fetch.
fix
Update code to call fetchRetry(fetch) instead of using fetchRetry directly.
affects: >=5.0.0 <6.0.0
Errors
Common errors & fixes
TypeError: fetchRetry is not a function
Incorrect import: used named import instead of default import in ESM, or wrong require() in CJS.
fix
Use default import: import fetchRetry from 'fetch-retry'; or require: const fetchRetry = require('fetch-retry');
Cannot find module 'fetch-retry' or 'fetch-retry' is not defined
Missing npm install or incorrect import path.
fix
Run 'npm install fetch-retry'. Ensure import path is 'fetch-retry' without any subpath.
fetch is not a function when calling fetchRetry(fetch)
The fetch parameter passed to fetchRetry is not a valid function (e.g., undefined).
fix
Ensure you pass a fetch implementation like cross-fetch, node-fetch, or global.fetch.
UnhandledPromiseRejectionWarning: TypeError: Failed to fetch
Network error occurred and retries exhausted, or fetch implementation not compatible.
fix
Check network connectivity. Ensure the fetch polyfill supports the environment (e.g., browser vs Node).
Upgrade
Version history
6.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetch-retry — npm install fetch-retry · libregistry