After upgrading to the latest version of Axios, the build fails in environments using Webpack 4 or older bundlers that do not support ES2020 syntax (specifically the Nullish Coalescing operator ??).
The error occurs in lib/adapters/fetch.js at the following line:
const globalObject = utils.global ?? globalThis;
Error Log
Module parse failed: Unexpected token (30:37)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
|
| const factory = (env) => {
> const globalObject = utils.global ?? globalThis;
| const { ReadableStream, TextEncoder } = globalObject;
Environment
- Axios version: latest
- Environment:
Vue 2.7
- Bundler:
Webpack 4.x
- Node.js version:
v20.19.6
To Reproduce
- Create a Vue 2.7 project with Webpack 4.
- Install the latest version of Axios.
- Import Axios in any component.
- Run
npm run serve or npm run build.
Expected behavior
Axios should be compatible with widely used build tools (like Webpack 4) or provide a transpiled version (ES5/ES6) in its distribution package to avoid breaking legacy-but-supported environments like Vue 2.7.
Suggested Fix / Possible Workaround
While users can add Axios to transpileDependencies in vue.config.js, it would be better for library compatibility if the code used more widely supported syntax, such as:
const globalObject = utils.global !== undefined && utils.global !== null ? utils.global : globalThis;
Or ensure the published build targets a more compatible ES version.
After upgrading to the latest version of Axios, the build fails in environments using Webpack 4 or older bundlers that do not support ES2020 syntax (specifically the Nullish Coalescing operator
??).The error occurs in
lib/adapters/fetch.jsat the following line:Error Log
Environment
Vue 2.7Webpack 4.xv20.19.6To Reproduce
npm run serveornpm run build.Expected behavior
Axios should be compatible with widely used build tools (like Webpack 4) or provide a transpiled version (ES5/ES6) in its distribution package to avoid breaking legacy-but-supported environments like Vue 2.7.
Suggested Fix / Possible Workaround
While users can add Axios to
transpileDependenciesinvue.config.js, it would be better for library compatibility if the code used more widely supported syntax, such as:Or ensure the published build targets a more compatible ES version.