Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions core/packages/gaxios/src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,27 @@ export class Gaxios implements FetchCompliance {
static async #getFetch() {
const hasWindow = typeof window !== 'undefined' && !!window;

this.#fetch ||= hasWindow
? window.fetch
: (await import('node-fetch')).default;
if (!this.#fetch) {
if (hasWindow) {
this.#fetch = window.fetch;
} else if (typeof globalThis.fetch === 'function') {
// Prefer native fetch when available (Node.js 18+).
//
// Dynamically importing `node-fetch` can fail in newer Node.js
// runtimes (e.g. 24.15+ with undici >=7.24.4) because internal changes
// to the native fetch implementation cause `import("node-fetch").default`
// to return a non-callable value, resulting in:
// TypeError: fetchImpl is not a function
//
// Node.js 18+ ships a stable global `fetch` that is fully compatible
// with the Fetch API, so we prefer it and avoid the `node-fetch`
// import entirely when it is available.
this.#fetch = globalThis.fetch.bind(globalThis);
} else {
// Fallback: older Node.js versions without a global fetch.
this.#fetch = (await import('node-fetch')).default;
}
}

return this.#fetch;
}
Expand Down
Loading