
Node Fetch Premature Close After Node 24.17 - The Real Fix
Skilldham
Engineering deep-dives for developers who want real understanding.
Last updated: November 2026
TL;DR
The node fetch premature close error is a Node.js bug, not a network problem.
The June 18, 2026 security release changed how idle keep-alive sockets work. node-fetch@2 misread that change and threw false errors. Node fixed it within a week.
Upgrade to Node 22.23.1+, 24.18.0+ or 26.4.0+
Check for pinned images, cached CI runners and bundled runtimes
Remove any engines upper bound you added to dodge the bug
The Error That Looks Like a Network Problem
Your service worked yesterday. Today every Google OAuth call fails.
The log says this:
bash
# The exact error from node-fetch@2
FetchError: Invalid response body while trying to fetch
https://oauth2.googleapis.com/token: Premature close
at Gunzip.<anonymous> (node_modules/node-fetch/lib/index.js:400:12)
type: 'system',
errno: 'ERR_STREAM_PREMATURE_CLOSE',
code: 'ERR_STREAM_PREMATURE_CLOSE'You did not change any network code. So you blame the host, the proxy or the API.
You add retries. You raise timeouts. Some calls pass, most still fail.
Here is the truth. The node fetch premature close error came from a Node.js patch.
It hit Node 22.23.0, 24.17.0 and the matching 26.x release. Your network is fine.
Who sees this error
You see it if anything in your app uses node-fetch@2. That is a lot of apps.
Google’s gaxios uses it. So do google-auth-library, firebase-tools and cross-fetch. The HubSpot client and the Changesets CLI hit it too.
Most teams never installed node-fetch directly. It arrived as a dependency of a dependency.
What the error does not mean
“Premature close” sounds like the server dropped you. It did not.
The full response often arrived. node-fetch just decided it was cut short. So retries and longer timeouts cannot fix it.
What Actually Broke in Node 22.23.0 and 24.17.0
On June 18, 2026, Node shipped a security release for 22.x, 24.x and 26.x. It fixed CVE-2026-48931, a response queue poisoning issue in http.Agent.
The fix guarded idle keep-alive sockets. It did that by attaching a public 'data' listener to them.
That small detail broke node-fetch@2.
How node-fetch@2 detects a cut response
node-fetch@2 checks chunked responses for truncation. When the socket closes, it counts the 'data' listeners on it.
If the count is above zero, it assumes data was still coming. Then it throws ERR_STREAM_PREMATURE_CLOSE.
javascript
// Simplified: the check inside node-fetch@2 that misfired
socket.prependListener('close', function onSocketClose(hadError) {
// Before the patch, an idle socket had zero 'data' listeners here.
// After the patch, Node's own guard listener was still attached.
const hasDataListener = socket.listenerCount('data') > 0;
if (hasDataListener && !hadError) {
const err = new Error('Premature close');
err.code = 'ERR_STREAM_PREMATURE_CLOSE';
response.body.emit('error', err);
}
});Why keep-alive makes it worse
Keep-alive reuses one socket for many requests. That reuse is where the guard listener lives.
So the first request often works. The second one on the same socket fails.
That is why the bug looked random. It also explains why a fresh curl always worked.
How Node fixed it
Node shipped PR #64004 a few days later. It moved the guard to an internal onread hook.
No public listener means node-fetch@2 counts zero again. The false errors stop.
The fix landed in Node 22.23.1, 24.18.0 and 26.4.0. Node 22.23.1 shipped on June 23, 2026.

Check If You Are Affected in 60 Seconds
Do not guess. Run two commands in the same place your code runs.
That means inside the Docker container or the CI job. Your laptop may run a different Node.
Step 1 - check your Node version
bash
# Print the exact runtime version
node -p "process.version"
# Affected: v22.23.0, v24.17.x, and 26.x below v26.4.0
# Fixed: v22.23.1+, v24.18.0+, v26.4.0+Step 2 - check for node-fetch@2
bash
# Show every copy of node-fetch in the dependency tree
npm ls node-fetch --all
# pnpm users
pnpm why node-fetchIf you see node-fetch@2.x and an affected Node, you found your bug.
Step 3 - add a startup guard
This check fails fast at boot. It turns a vague runtime error into a clear message.
javascript
// Correct: warn at startup if this Node version has the node-fetch regression
// File: check-node.js - require it first in your entry file
const [major, minor, patch] = process.versions.node.split('.').map(Number);
function hasPrematureCloseBug() {
if (major === 22) return minor === 23 && patch === 0;
if (major === 24) return minor === 17;
if (major === 26) return minor < 4;
return false;
}
if (hasPrematureCloseBug()) {
throw new Error(
`Node ${process.version} breaks node-fetch@2 keep-alive. ` +
'Upgrade to 22.23.1+, 24.18.0+ or 26.4.0+.'
);
}The 26.x check is broad on purpose. Any 26.x below 26.4.0 also misses later security fixes.
Fix 1 - Upgrade Node and Pin the Right Way
Upgrading Node is the real fix. Everything else is a stopgap.
The catch is where the old version hides.
Docker images pinned to an affected tag
dockerfile
# Wrong: pinned to the broken patch
FROM node:24.17.0-slimThis image keeps the bug forever. Rebuilds will not help.
dockerfile
# Correct: pin to a patched release, then bump on each security release
FROM node:24.22.1-slimPinning is good practice. Just pin to a fixed version, not a broken one.
A Node bump can also break native modules in the same image. The Node 24 native module Docker build fix covers that layer order.
CI runners with a cached Node
GitHub’s Ubuntu 24.04 runner cached Node 22.23.0 for a while. node-version: 22 could resolve to that cached copy.
yaml
# Wrong: may resolve to an old cached 22.x on the runner
- uses: actions/setup-node@v4
with:
node-version: 22yaml
# Correct: always fetch the newest 22.x release
- uses: actions/setup-node@v4
with:
node-version: 22
check-latest: truecheck-latest: true skips the stale cache. It downloads the newest matching release.
Hosts that pick Node for you
Railway and similar hosts read your engines field. With no pin, a rebuild can jump you onto a new patch.
That is how many teams hit this on June 19. Nothing in their code changed.
Fix 2 - Replace node-fetch With Native fetch
Node has a global fetch since Node 18. It runs on undici, not on http.Agent.
So the June patch never touched it. Moving to native fetch removes this whole bug class.
The swap for your own code
javascript
// Wrong: node-fetch@2 with the old timeout option
const fetch = require('node-fetch');
const res = await fetch('https://api.example.com/data', { timeout: 5000 });
const data = await res.json();javascript
// Correct: native fetch with AbortSignal for the timeout
const res = await fetch('https://api.example.com/data', {
signal: AbortSignal.timeout(5000),
});
if (!res.ok) {
throw new Error(`Request failed: ${res.status}`);
}
const data = await res.json();Native fetch has no timeout option. AbortSignal.timeout() is the direct replacement.
Gotchas when you switch
res.buffer() does not exist in native fetch. Use Buffer.from(await res.arrayBuffer()) instead.
Native fetch does not take an agent option either. Undici uses a dispatcher instead.
The Node 26 fetch dispatcher error fix covers that setup. It is the next wall people hit after the swap.
When you cannot swap
You cannot edit code inside gaxios or firebase-tools. For those, upgrade Node.
Do not force node-fetch@3 with npm overrides. Version 3 is ESM only, so CommonJS callers break.
Fix 3 - Disable Keep-Alive as a Stopgap
Sometimes you cannot upgrade Node today. A frozen base image or a bundled runtime can block you.
Turning off keep-alive avoids socket reuse. No reuse means no leftover guard listener.
Pass a non-keep-alive agent
javascript
// Correct (stopgap): force a fresh socket for every node-fetch@2 request
const https = require('node:https');
const fetch = require('node-fetch');
const noKeepAliveAgent = new https.Agent({ keepAlive: false });
const res = await fetch('https://api.example.com/data', {
agent: noKeepAliveAgent,
});Every request now opens a new TLS connection. That costs latency on busy services.
Treat this as a bridge. Remove it once Node is patched.
Runtimes you do not control
Electron apps ship their own Node. VS Code 1.128.0 ships Node 24.17.0 inside Electron 42.5.0.
The GitLab VS Code extension broke there for this reason. It shipped a keep-alive toggle as its fix.
If you build Electron apps, check the bundled Node version. Your package.json cannot change it.
The Second Bug: Pinning Node to Escape the First
This is the part nobody talks about. Many teams fixed the error by pinning Node backwards.
That worked in June. By October, it had become a security hole.
The engines range that blocks security patches
json
{
"//": "Wrong: an upper bound added to dodge the June regression",
"engines": {
"node": ">=22.22.0 <22.23.0"
}
}This range blocks the bug. It also blocks every later 22.x security release.
The Outline project hit exactly this. Its upper bounds excluded the October releases, 26.12.1 and 24.22.1.
The correct engines range
json
{
"//": "Correct: exclude only the broken patches, allow every later fix",
"engines": {
"node": ">=22.23.1 <23 || >=24.18.0 <25 || >=26.4.0"
}
}This skips the three broken lines. It still allows every security patch after them.
Search your repos for <22.23, <24.17 and 24.16.0. Those are signs of an old workaround.
Why this matters now
Node 26 became Active LTS on October 28, 2026. Teams are planning upgrades right now.
An old pin will quietly hold you back. The Node.js Brotli RCE breakdown shows why current security patches matter.
Why Retries and Timeouts Never Fixed It
Most advice online treats this as a flaky network. That advice wastes hours.
Retries hide the bug
A retry may land on a fresh socket and pass. Then the next reused socket fails again.
You get a lower error rate, not a fix. Your logs still fill with noise.
Timeouts change nothing
The response did not take too long. node-fetch rejected it after it finished.
A larger timeout just waits longer before the same false error.
The Next.js 16.3 MaxListenersExceededWarning fix teaches the same lesson. When listener counts look wrong, check the runtime first.
Diagram comparing a reused keep-alive socket before and after the Node 24.17.0 patch
Key Takeaways
The node fetch premature close error after June 18, 2026 is a Node.js bug.
Affected versions are 22.23.0, 24.17.x and 26.x below 26.4.0.
Fixed versions are 22.23.1, 24.18.0 and 26.4.0 or newer.
Run node -p "process.version" inside the container, not on your laptop.
Use npm ls node-fetch --all to find hidden node-fetch@2 copies.
Move your own code to native fetch with AbortSignal.timeout().
Disable keep-alive only as a short stopgap.
Remove any engines upper bound that blocks later Node security releases.
FAQ
Is the node fetch premature close error caused by my network?
No, not when it starts on Node 22.23.0 or 24.17.0. node-fetch@2 misreads a listener that Node’s security patch added. A real network drop looks the same, so check your Node version first.
Which Node versions have the premature close regression?
Node 22.23.0, Node 24.17.x and the matching 26.x security release are affected. The fix shipped in 22.23.1, 24.18.0 and 26.4.0. Any newer release on those lines is safe.
Does Node 26 LTS still have this bug?
No. Node 26 became LTS with 26.13.0 in October 2026, long after the 26.4.0 fix. If you run any current 26.x release, this regression is gone.
Why does curl work but my Node app fails?
curl opens a fresh connection each time. node-fetch@2 reuses keep-alive sockets, and the reused socket carries the bad listener. That is why the first request often works and later ones fail.
Can I fix it by setting a larger timeout?
No. The response already arrived in full. node-fetch rejects it after the fact, so a longer timeout only delays the same error.
Should I upgrade to node-fetch@3 instead?
Only if your code is ESM. node-fetch@3 is ESM only and breaks CommonJS callers. For most apps, native fetch is the better move.
My dependency uses node-fetch@2. What can I do?
Upgrade Node to a fixed release. You cannot change the fetch calls inside gaxios or firebase-tools. Upgrading the runtime fixes every copy at once.
Is it safe to pin Node to 24.16.0 to avoid the bug?
It was a fair stopgap in June 2026, but not today. 24.16.0 misses every security fix since then. Pin to 24.18.0 or newer instead.
Conclusion
The node fetch premature close error was never your network. It was a one-line side effect of a Node security patch.
Understanding the cause saves you from retries, timeout hacks and backwards pins. Upgrade Node, find hidden node-fetch@2 copies, and clean up old engines ranges.
Memory trouble after the upgrade? Read the Node 24.19 out of memory fix next.
Want fixes like this before they hit your production logs? Subscribe to the SkillDham newsletter for real Node.js and Next.js breakages, explained.