Axios
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
Axios is a promise-based HTTP client for both browser and Node.js. The current stable version is 1.0.0. It provides a simple API for making XMLHttpRequests in the browser and http requests in Node.js, with features like interceptors, automatic JSON transformation, and request cancellation. Axios is widely used due to its ease of use, support for older browsers (IE11), and TypeScript types. It follows semver and has a consistent release cadence with minor updates every few months.
Common errors
error TypeError: Cannot read properties of undefined (reading 'data') ↓
cause Trying to access response.data when request failed and error.response is undefined.
fix
Check if error.response exists before accessing data: if (error.response) { console.log(error.response.data); }
error Module not found: Error: Can't resolve 'axios' ↓
cause Axios is not installed or incorrectly imported in a browser environment.
fix
Run
npm install axios and ensure the import path is correct. For CDN, use script tag. error Uncaught (in promise) Error: Network Error ↓
cause Request failed due to network issues (e.g., CORS, no internet, blocked request).
fix
Check browser console for CORS errors, ensure endpoint is reachable, and configure CORS on server.
error TypeError: _axios.default.get is not a function (or axios.get is not a function) ↓
cause Using import axios from 'axios' in a CommonJS module without esModuleInterop.
fix
Use
const axios = require('axios') in Node.js CJS or enable esModuleInterop in tsconfig. Warnings
breaking Starting from v0.22.0, Axios supports ES modules and has changed default export behavior. ↓
fix Use import axios from 'axios' instead of require('axios'). In TypeScript, use esModuleInterop.
deprecated The `adapter` config option is deprecated in favor of `httpAdapter` and `xhrAdapter`. ↓
fix Use `httpAdapter` for Node.js or `xhrAdapter` for browser instead of `adapter`.
gotcha Axios v1 changed the default response type from `text` to `json` for all requests, including those that return non-JSON content. ↓
fix Set `responseType: 'text'` explicitly if expecting non-JSON responses, or use `transformResponse`.
breaking Initial release of extrareqp2 is a fork of axios v1.0.0? Check version history. ↓
fix Refer to axios documentation for compatibility; extrareqp2 may have unknown breaking changes.
deprecated The `CancelToken` API is deprecated; use `AbortController` instead. ↓
fix Use AbortController from the platform (browser or Node.js 16+) to cancel requests.
gotcha axios.post(url, data, config) does NOT automatically stringify data; set `Content-Type` header manually if needed. ↓
fix For JSON, ensure data is an object and Axios will set Content-Type: application/json automatically. For form data, use URLSearchParams or FormData.
Install
npm install extrareqp2 yarn add extrareqp2 pnpm add extrareqp2 Imports
- axios wrong
const axios = require('axios')correctimport axios from 'axios' - AxiosInstance wrong
const { AxiosInstance } = require('axios')correctimport axios, { AxiosInstance } from 'axios' - AxiosRequestConfig wrong
const { AxiosRequestConfig } = require('axios')correctimport axios, { AxiosRequestConfig } from 'axios' - AxiosResponse wrong
import { response } from 'axios'correctimport { AxiosResponse } from 'axios'
Quickstart
import axios from 'axios';
async function main() {
try {
const response = await axios.get('https://api.example.com/data', {
params: { key: process.env.API_KEY ?? '' },
timeout: 5000
});
console.log(response.data);
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Axios error:', error.message);
} else {
console.error('Unexpected error:', error);
}
}
}
main();