Skip to content
Latchkey

Node "ERR_INVALID_THIS" (undici fetch) in CI

undici (the engine behind global fetch) threw ERR_INVALID_THIS because a Headers/Request method was called with the wrong receiver - typically a fetch polyfill clashing with the built-in global on an older Node.

What this error means

A network call or a tool using fetch fails with "TypeError [ERR_INVALID_THIS]: Value of this must be of type Headers". It often appears after a dependency upgrade or on a pinned older Node version.

node
TypeError [ERR_INVALID_THIS]: Value of "this" must be of type Headers
    at Headers.append (node:internal/deps/undici/undici:...)

Common causes

Polyfill clashing with built-in fetch

A fetch/Headers polyfill is loaded over the native global, so methods receive an incompatible this.

Outdated Node with a newer undici expectation

A library assumes a newer undici/fetch than the pinned Node ships, triggering the receiver check.

How to fix it

Use a supported Node and drop the polyfill

Run a Node version with native fetch and remove redundant fetch polyfills.

workflow
# .nvmrc / workflow
node-version: 20
# remove node-fetch / whatwg-fetch polyfills when on Node 18+

Align undici across dependencies

If a library pins its own undici, dedupe so a single compatible version is used.

Terminal
npm dedupe
npm ls undici

How to prevent it

  • Use native fetch on Node 18+ instead of polyfills.
  • Keep a single undici version via dedupe.
  • Pin a Node version that matches your libraries fetch expectations.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →