Bundling Axios with Webpack, Rollup, and Vite

Bundling the Axios HTTP client into modern web applications requires addressing module resolution, target environments, dependency polyfills, and bundle size optimization. While Axios is versatile and works in both Node.js and browser environments, differences in how Webpack, Rollup, and Vite process packages mean developers must account for specific configurations to avoid bloated builds or runtime errors.

Module Format and Resolution (ESM vs. CommonJS)

Axios provides both CommonJS and ECMAScript Module (ESM) builds. Modern bundlers rely on the exports field in package.json to determine the correct entry point:

Platform Target and Adapter Selection

Axios automatically chooses between two adapters: the Node.js adapter (using http/https modules) and the browser adapter (using XMLHttpRequest or fetch). Bundlers must be instructed which environment is being targeted.

Bundler-Specific Configurations

Webpack

Rollup

Vite

Bundle Size and Tree-Shaking Considerations

Axios is architected around a single, highly configurable core instance. As a result: