fix(deps): patch gaxios to use globalThis.fetch on Node.js 18+ (fixes #32245)#33116
fix(deps): patch gaxios to use globalThis.fetch on Node.js 18+ (fixes #32245)#33116whongj wants to merge 1 commit intoopenclaw:mainfrom
Conversation
gaxios 7.1.x checks for `window` to decide whether to use native fetch.
In Node.js (no `window`), it falls back to `await import('node-fetch')`.
On Node.js v22.22.0, this dynamic ESM import crashes the module loader:
TypeError: Cannot convert undefined or null to object
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:213:12)
This kills `google-auth-library` JWT token exchange before any Vertex AI
call is made, causing ALL Vertex-backed embedded runs to fail with:
embedded run agent end: isError=true error=Cannot convert undefined or null to object
Node.js 18+ ships native `fetch` as `globalThis.fetch`. The fix is a
one-liner: check `globalThis.fetch` before attempting the broken ESM import.
The postinstall script `scripts/patch-gaxios-node22.mjs` applies this
patch idempotently after `npm install`. It is a no-op if gaxios has been
fixed upstream or if the pattern has changed.
Fixes: openclaw#32245
Upstream issue: googleapis/gaxios (pending PR)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 369ac04eb2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| const content = fs.readFileSync(gaxiosPath, "utf8"); | ||
|
|
||
| const BROKEN = ": (await import('node-fetch')).default;"; |
There was a problem hiding this comment.
Match gaxios import string before patching
The patcher never applies on the affected gaxios@7.1.3 builds because BROKEN looks for import('node-fetch') (single quotes), while the compiled file uses import("node-fetch") in #getFetch(); this makes content.includes(BROKEN) false and exits with "Pattern not found", so installs remain vulnerable to the Node 22 crash this commit is intended to fix.
Useful? React with 👍 / 👎.
Greptile SummaryThis PR adds a The core fix logic is sound and addresses a real blocker for Vertex AI embedded runs on Node.js v22+. The idempotency checks (already patched, pattern not found) are well-thought-out. One minor cleanup: the Confidence Score: 4/5
Last reviewed commit: 369ac04 |
| * Fix: https://github.com/googleapis/gaxios/issues/<upstream> | ||
| */ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; |
There was a problem hiding this comment.
path is imported but never referenced in the script. Remove unused import:
| import path from "node:path"; | |
| import { createRequire } from "node:module"; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: scripts/patch-gaxios-node22.mjs
Line: 10
Comment:
`path` is imported but never referenced in the script. Remove unused import:
```suggestion
import { createRequire } from "node:module";
```
How can I resolve this? If you propose a fix, please make it concise.
Summary
Fixes #32245 —
Cannot convert undefined or null to objecton all Vertex AI embedded runs (Node.js v22+).Root Cause
gaxios7.1.x decides whichfetchimplementation to use in its private static#getFetch():In a Node.js environment,
windowisundefined, so it falls back toawait import("node-fetch"). On Node.js v22.22.0, this dynamic ESM import ofnode-fetchv3.3.2 crashes the ESM module loader:This crashes
google-auth-libraryJWT token exchange before any Vertex AI API call is made, causing every Vertex-backed embedded run to fail with:Node.js v18+ ships native
fetchasglobalThis.fetch. gaxios never checks for it.Fix
A one-liner postinstall script (
scripts/patch-gaxios-node22.mjs) that idempotently patchesgaxios/build/cjs/src/gaxios.js:The script:
postinstallinpackage.jsonVerification
All Vertex AI models (
gemini-3-flash-preview,gemini-2.0-flash, etc.) resume working normally.Notes
googleapis/gaxios— a PR has been filed therev22.22.0, Debian 12, OpenClaw2026.3.2