Skip to content

fix(gaxios): prefer globalThis.fetch over import('node-fetch') on Node.js 18+#916

Open
whongj wants to merge 1 commit intogoogleapis:mainfrom
whongj:fix/gaxios-prefer-globalthis-fetch
Open

fix(gaxios): prefer globalThis.fetch over import('node-fetch') on Node.js 18+#916
whongj wants to merge 1 commit intogoogleapis:mainfrom
whongj:fix/gaxios-prefer-globalthis-fetch

Conversation

@whongj
Copy link

@whongj whongj commented Mar 3, 2026

Problem

Node.js v22.22.0 crashes when gaxios executes await import("node-fetch") from a CJS context:

TypeError: Cannot convert undefined or null to object
    at hasOwnProperty (<anonymous>)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:213:12)

This kills google-auth-library JWT token exchange, breaking all Vertex AI calls (@google/genai, googleapis/*) on Node.js v22+.

Root Cause

In #getFetch(), the code checks for window (browser detection) but never checks globalThis.fetch, which has been available natively since Node.js 18:

static async #getFetch() {
    const hasWindow = typeof window !== 'undefined' && !!window;
    this.#fetch ||= hasWindow
      ? window.fetch
      : (await import('node-fetch')).default;  // ← crashes Node v22
}

Fix

Check globalThis.fetch before falling back to node-fetch. One line:

-      : (await import('node-fetch')).default;
+      : (typeof globalThis.fetch === 'function' ? globalThis.fetch : (await import('node-fetch')).default);

Verification

# Node.js v22.22.0 — before:
node -e "const {Gaxios}=require('gaxios');new Gaxios().request({url:'https://example.com'}).catch(e=>console.error(e.message))"
# TypeError: Cannot convert undefined or null to object

# After patch:
# 200

Also verified: google-auth-library JWT.getAccessToken() succeeds after patch on Node.js v22.22.0.

Related

…e.js 18+

Node.js v22.22.0 crashes when gaxios tries `await import('node-fetch')`:

  TypeError: Cannot convert undefined or null to object
      at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:213:12)

This causes google-auth-library JWT exchange to fail, breaking all
Vertex AI usage (GoogleGenAI, googleapis/*) on Node.js v22+.

Root cause: `#getFetch()` checks for `window` (browser) but ignores
`globalThis.fetch`, available natively since Node.js 18.

Fix: check `typeof globalThis.fetch === 'function'` first.

Reported in: openclaw/openclaw#32245
@whongj whongj requested a review from a team as a code owner March 3, 2026 14:46
@google-cla
Copy link

google-cla bot commented Mar 3, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant