middleware-axios is a JavaScript and TypeScript library that extends the popular Axios HTTP client, providing an Express or Koa-like middleware interface for intercepting and modifying HTTP requests and responses. The current stable version is 3.0.0, which introduced dual CommonJS and ESM support and updated its peer dependency to Axios `^1.7.4`. The project demonstrates an active release cadence, with major versions addressing significant architectural changes like module system compatibility and dependency updates, while patch releases focus on type fixes and minor improvements. Its key differentiator lies in abstracting the native Axios interceptor pattern into a more structured, sequential middleware pipeline, allowing developers to easily add global or instance-specific pre-request and post-response logic, access `axios.defaults`, and control the flow with a `next()` function call, making request handling more modular and maintainable.
npm install middleware-axiosVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a `middleware-axios` instance, add a basic logging middleware that intercepts requests and responses, and then make a GET request using the wrapped instance.
Ensure your build tools and Node.js environment are configured for ESM imports (e.g., using `import { create } from 'middleware-axios';`). If using CommonJS, explicit `require` might still work, but ESM is the recommended modern approach. Review your `package.json` for `type: 'module'`.Upgrade your `axios` installation to version `^1.7.4` or higher using `npm install axios@^1.7.4` or `yarn add axios@^1.7.4`.
Always include `await next(config);` at the appropriate point in your middleware to ensure the request proceeds through the chain.
For TypeScript users, ensure `instanceConfig` parameters are typed as `CreateAxiosDefaults` for compatibility with Axios v1.x types.
Always ensure your installed `axios` version meets or exceeds the minimum peer dependency specified by your `middleware-axios` version. For `middleware-axios@2.1.x`, refer to its specific `package.json` for the exact `axios` peer dependency.
For Node.js projects, ensure `type: "module"` is set in your `package.json` if using ESM `import` statements, or explicitly use dynamic `import()` for ESM modules in a CJS context. For `middleware-axios` v3, ensure you are using `import { create } from 'middleware-axios';`.Always import symbols directly from the main package entry point: `import { create } from 'middleware-axios';`. Do not attempt to import from internal paths.Install or upgrade `axios` to a compatible version: `npm install axios@^1.7.4` or `yarn add axios@^1.7.4`.