Axios Request Retries
retry-axios is a library designed to automatically retry failed HTTP requests made using the Axios client library. It operates as an Axios interceptor, providing built-in exponential backoff and extensive configuration options for retry logic, including HTTP methods, status codes, retry delays, and jitter strategies. The current stable version is 4.0.3, released in April 2026. The package demonstrates a consistent release cadence, with major versions aligning with significant Node.js LTS updates. Key differentiators include its deep integration with Axios interceptors, allowing for global or instance-specific retry policies, and its flexible backoff algorithms (exponential, static, linear) to prevent thundering herd problems, making it suitable for robust client-side network operations in both Node.js and browser environments.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'attach')
cause Incorrect ESM import of the `retry-axios` library, often using a default import instead of a namespace import.fixChange `import rax from 'retry-axios';` to `import * as rax from 'retry-axios';`. -
Error: ENODATA: no data: getaddrinfo EAI_AGAIN
cause Network error indicating a failure to resolve the hostname. While not directly caused by `retry-axios`, it's a common error that `retry-axios` is designed to handle.fixEnsure the target URL is correct and reachable. If it's an intermittent issue, `retry-axios` with its default configuration will attempt to retry, potentially resolving the issue on subsequent attempts. Check DNS settings and network connectivity. -
ERR_BAD_RESPONSE: Request failed with status code 503
cause The server responded with a 5xx status code (e.g., Service Unavailable), which is typically a retryable error that `retry-axios` is configured to handle by default.fixThis error itself is expected to be caught and retried by `retry-axios`. If retries are not happening, verify that `rax.attach()` was called and that `raxConfig.statusCodesToRetry` includes the relevant 5xx range. Increase `raxConfig.retry` if more attempts are needed.
Warnings
- breaking Version 4.0.0 and above of `retry-axios` require Node.js 20 or higher. Previous versions supported older Node.js runtimes. Ensure your environment meets this minimum version requirement.
- gotcha When importing `retry-axios` with ES Modules, the correct pattern is `import * as rax from 'retry-axios';` because the library exports a namespace object. A direct default import like `import rax from 'retry-axios';` will result in `rax` being undefined or incorrect.
- gotcha `retry-axios` can be attached globally to the default `axios` instance or to a specific `axios.create()` instance. Attaching to the global instance affects all requests made through `axios`, while attaching to a custom instance scopes the retry logic only to that instance.
- breaking The `retry-axios` configuration for an Axios instance is set via `myAxiosInstance.defaults.raxConfig`. Prior to v3, some configuration might have been set directly on the `axios` instance. Ensure you use the `raxConfig` property.
Install
-
npm install retry-axios -
yarn add retry-axios -
pnpm add retry-axios
Imports
- rax
import rax from 'retry-axios';
import * as rax from 'retry-axios';
- attach
import rax, { attach } from 'retry-axios';import { attach } from 'retry-axios'; - rax (CommonJS)
const rax = require('retry-axios'); - AxiosRequestConfig (Type)
import { AxiosRequestConfig } from 'axios'; import { RaxConfig } from 'retry-axios';
Quickstart
import * as rax from 'retry-axios';
import axios, { AxiosInstance } from 'axios';
interface MyRaxConfig extends rax.RaxConfig {
// Optionally extend raxConfig if needed
}
const myAxiosInstance: AxiosInstance = axios.create();
// Configure retry-axios for this specific instance
myAxiosInstance.defaults.raxConfig = {
retry: 5, // Retry 5 times
retryDelay: 500, // 500ms delay between retries
statusCodesToRetry: [[429, 429], [500, 599]], // Only retry for Too Many Requests and 5xx errors
backoffType: 'exponential', // Use exponential backoff
jitter: 'full', // Add full jitter
onError: (err) => {
const cfg = rax.getConfig(err);
if (cfg) {
console.log(`Retry attempt #${cfg.currentRetryAttempt + 1} for URL: ${err.config?.url}`);
}
}
} as MyRaxConfig;
// Attach the retry interceptor to the custom instance
rax.attach(myAxiosInstance);
async function fetchData() {
try {
// Simulate an API call that might fail and need retries
// Using httpbin.org for demonstration purposes.
// In a real scenario, this might be a flaky backend endpoint.
const response = await myAxiosInstance.get('https://httpbin.org/status/503');
console.log('Successfully fetched data:', response.data);
} catch (error: any) {
if (error.response) {
console.error(`Failed to fetch data after retries. Status: ${error.response.status}`);
} else if (error.request) {
console.error('Failed to fetch data after retries. No response received.');
} else {
console.error('Error during request setup:', error.message);
}
}
}
fetchData();