Skip to content

fix(fetch): defer global access to avoid module-load TypeError#7260

Merged
jasonsaayman merged 1 commit intoaxios:v1.xfrom
Jye10032:fix/global-undefined-issue-7259
May 1, 2026
Merged

fix(fetch): defer global access to avoid module-load TypeError#7260
jasonsaayman merged 1 commit intoaxios:v1.xfrom
Jye10032:fix/global-undefined-issue-7259

Conversation

@Jye10032
Copy link
Copy Markdown
Contributor

@Jye10032 Jye10032 commented Nov 25, 2025

Description

Fixes #7259

Similar to #7153 which fixed unsafe destructuring in config.env, this PR fixes another unsafe destructuring pattern in the fetch adapter that causes TypeError when utils.global is undefined during module loading.

Problem

The current code (lines 15-17 in lib/adapters/fetch.js) directly destructures utils.global without checking if it's defined:

const globalFetchAPI = (({Request, Response}) => ({
  Request, Response
}))(utils.global);

This throws TypeError: Cannot destructure property 'Request' of 'undefined' in certain build environments (Vite, webpack) where module initialization timing can cause utils.global to be temporarily undefined.

Summary

Defer utils.global access to factory() and guard it with a fallback object to prevent module-
load crashes when globals are not ready.

Fix

  • Move Request/Response/ReadableStream/TextEncoder access into factory()
  • Use a fallback object to avoid destructuring from undefined

Reproduction

Minimal repro repo:
https://github.com/Jye10032/axios-repo

Steps:

  1. npm install
  2. npm run build
  3. npm run preview
  4. Open the preview URL → console shows:

TypeError: Cannot destructure property 'Request' of 'undefined'

Related

@Jye10032 Jye10032 force-pushed the fix/global-undefined-issue-7259 branch from ed56157 to cdc3bb9 Compare December 22, 2025 07:27
@alibassam24
Copy link
Copy Markdown

gg well played

@Jye10032 Jye10032 force-pushed the fix/global-undefined-issue-7259 branch from 8a9a5a8 to 92d03d5 Compare January 26, 2026 07:37
Copilot AI review requested due to automatic review settings January 26, 2026 07:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents a module-load TypeError in the fetch adapter by making destructuring from utils.global safe when it is undefined (as reported in #7259).

Changes:

  • Add a default value (= {}) to the destructured parameter used to extract Request/Response from utils.global.
  • Guard ReadableStream/TextEncoder destructuring by falling back to {} when utils.global is falsy.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Copy link
Copy Markdown
Member

@jasonsaayman jasonsaayman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Jye10032. When fetch.js's top-level code runs before utils.js's default export has finished populating (a circular-import / evaluation-order quirk that bundlers like Vite can surface), utils.global reads as undefined, and the original destructure has no default, so it throws at module load before anything in axios can recover. Moving the read into factory() sidesteps that entirely; by the time factory is invoked from getFetch(), the ESM graph has settled.

Two small simplifications before this lands.

Flatten the fallback. utils.global is itself globalThis ?? self ?? window ?? global, so the only realistic case where it's undefined is the circular-import one and in that case globalThis is equally available. The ?? {} arm is unreachable in any environment that runs ESM:

const globalObject = utils.global ?? globalThis;

Drop the IIFE around Request/Response. With globalObject already in scope, the IIFE's only purpose (handling destructure-of-undefined at module load) is gone:

const { Request, Response } = globalObject;
const globalFetchAPI = { Request, Response };

Same shape going into the utils.merge.call(…, globalFetchAPI, env) below.

Optional but nice: a regression test that stubs utils.global to undefined, calls factory({}), and asserts no throw. The bundler-graph case is hard to reproduce in unit tests directly, but stubbing the module is a reasonable proxy.

@Jye10032 Jye10032 force-pushed the fix/global-undefined-issue-7259 branch from e899732 to dac3e73 Compare May 1, 2026 15:48
@Jye10032 Jye10032 requested a review from jasonsaayman May 1, 2026 15:52
@Jye10032
Copy link
Copy Markdown
Contributor Author

Jye10032 commented May 1, 2026

Rebased onto the latest v1.x and updated the PR based on your feedback.

Changes in the latest revision:

  • moved the utils.global read into factory()
  • simplified the fallback to utils.global ?? globalThis
  • removed the extra IIFE/default-destructure guard
  • added a regression test covering utils.global = undefined to ensure the fetch adapter no
    longer crashes during initialization

I also re-ran the local fetch adapter tests and verified the original repro no longer throws the
Request destructuring error.

@jasonsaayman jasonsaayman changed the title fix: add default parameter to prevent destructuring undefined in fetch adapter fix(fetch): defer global access to avoid module-load TypeError May 1, 2026
@jasonsaayman jasonsaayman merged commit e573499 into axios:v1.x May 1, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix priority::medium A medium priority status::changes-requested A reviewer requested changes to the PR status::needs-rebase The PR requires a rebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: Cannot destructure Request/Response from undefined utils.global in Vite builds (1.13.2)

4 participants