Skip to content

Build Error: Unexpected token '??' in lib/adapters/fetch.js (Compatibility issue with Webpack 4 / Vue 2.7) #10851

@SuperMarshtomp

Description

@SuperMarshtomp

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

  1. Create a Vue 2.7 project with Webpack 4.
  2. Install the latest version of Axios.
  3. Import Axios in any component.
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions