You are currently viewing Test Built-In Fetch Before Moving to Node.js 26 LTS

Test Built-In Fetch Before Moving to Node.js 26 LTS

When you move a server application from Node.js 24 to Node.js 26 LTS, run the same Fetch checks under both runtimes before changing your request code. Start with a successful JSON response, a non-success status, an empty response, a redirect, and an explicit timeout. This gives you a baseline you can compare with your application’s actual behavior.

The test for this guide probed both runtime lines against the same local HTTP server. A runtime upgrade can change the version of Undici bundled with Node, even when your application never lists undici in package.json.

Identify the runtime that actually makes the request

In the place where your application runs, print the Node and bundled Undici versions. Repeat the check in your local terminal, your build or test job, and your deployment environment when those differ. The command is safe to include in an ordinary development log because it only prints version strings.

node -p "JSON.stringify({ node: process.version, undici: process.versions.undici })"

The October 28, 2026 release marked the transition of Node.js 26 to LTS under the name Lithium. The Node.js LTS release notes say that the release updated the LTS metadata. The earlier Node.js 26.0.0 release notes document the major Undici upgrade. The LTS announcement itself is therefore a reason to revisit your pin, while the exact version you run tells you which client is in the process.

Look at your source before assuming every request goes through the same client. A call to globalThis.fetch exercises the global in the process. An explicit import { fetch } from 'undici' can use the installed Undici package instead. A request wrapper may substitute its own client or pass a dispatcher. If your real application uses one of those paths, keep the built-in test as a baseline and add a separate run of the wrapper. Updating Node and updating a package are distinct changes.

Run one loopback check under both Node versions

Node 24 and Node 26 run the same Fetch script against five loopback test cases.
Node 24 and Node 26 run the same Fetch script against five loopback test cases.

Save this as fetch-upgrade-check.mjs. It asks the operating system for an available port and binds only to 127.0.0.1. It uses no packages and makes no requests to external services. Run node fetch-upgrade-check.mjs under your existing runtime and again under the Node.js 26 patch you plan to deploy. Check the first output line before comparing everything else.

import assert from 'node:assert/strict';
import { once } from 'node:events';
import { createServer } from 'node:http';

const server = createServer((request, response) => {
  if (request.url === '/slow') {
    const timer = setTimeout(() => response.end('late'), 2_000);
    response.on('close', () => clearTimeout(timer));
    return;
  }
  if (request.url === '/redirect') {
    response.writeHead(302, { location: '/ok' }).end();
    return;
  }
  if (request.url === '/empty') {
    response.writeHead(204).end();
    return;
  }
  const found = request.url === '/ok';
  response.writeHead(found ? 200 : 404, {
    'content-type': 'application/json',
  });
  response.end(JSON.stringify({ protocol: request.httpVersion, found }));
});

server.listen(0, '127.0.0.1');
await once(server, 'listening');
const base = 'http://127.0.0.1:' + server.address().port;

try {
  console.log('Node', process.version, 'Undici', process.versions.undici);
  const ok = await fetch(base + '/ok');
  const details = await ok.json();
  assert.equal(ok.status, 200);
  assert.equal(details.found, true);
  console.log('success:', ok.status, 'HTTP/' + details.protocol);

  const missing = await fetch(base + '/missing');
  assert.equal(missing.ok, false);
  assert.equal(missing.status, 404);
  await missing.json();
  console.log('missing:', missing.status, 'ok=' + missing.ok);

  const empty = await fetch(base + '/empty');
  assert.equal(empty.status, 204);
  assert.equal(await empty.text(), '');
  console.log('empty:', empty.status, '(empty body)');

  const redirected = await fetch(base + '/redirect');
  assert.equal(redirected.redirected, true);
  assert.equal(new URL(redirected.url).pathname, '/ok');
  await redirected.json();
  console.log('redirect:', redirected.status, '/ok');

  try {
    await fetch(base + '/slow', { signal: AbortSignal.timeout(200) });
    assert.fail('The slow request should have timed out.');
  } catch (error) {
    assert.equal(error.name, 'TimeoutError');
    console.log('slow:', error.name);
  }
} finally {
  server.closeAllConnections();
  await new Promise((resolve) => server.close(resolve));
}

Save the full output from both runs. The paths are deliberately simple so you can tell whether a failure comes from a status check, a body assumption, redirects, or a timeout. A test that makes real API calls can fail for a host of other reasons, including changing test data or an expired sandbox token. Here the expected body and status come from code you control.

Observed results on Windows

The two runs reported Node v24.22.3 and Undici 7.30.2 and Node v26.13.1 and Undici 8.12.4. Both produced these same five result lines:

Terminal results show the same five Fetch outcomes on Node 24.22.3 and Node 26.13.1.
Terminal results show the same five Fetch outcomes on Node 24.22.3 and Node 26.13.1.
success: 200 HTTP/1.1
missing: 404 ok=false
empty: 204 (empty body)
redirect: 200 /ok
slow: TimeoutError

The bundled client changed, and these observed behaviors matched. Keep that conclusion limited to these routes. If a later run prints a different version than you expect, fix the runtime selection first. If only one assertion fails under the intended version, rerun that route in isolation before deciding whether the behavior changed. For example, loopback requests made under heavy CPU load can miss a tightly chosen timing budget.

Read each result before changing application code

A 404 is a response your application needs to handle

The 404 route returns a JSON response. The Fetch promise fulfills and response.ok is false. Treat these as two observations: a response arrived, and the status says your request did not succeed. If your wrapper only catches promise rejection, it may still treat a handled API error as usable data. Decide whether your application needs a typed error, a fallback page, or a clear message. Assert the behavior you intend rather than depending on a generic catch block.

Some APIs include a useful error object, and others return an HTML page or no body. If you choose to parse an error body, keep that parsing separate from your decision to treat the status as a failure. If you skip a response body in a server application, follow the Undici guidance on consuming or cancelling bodies so connection resources can be released.

Check a successful response for an actual body

Our empty route returns 204. The request succeeded, and there was nothing to parse. A helper that calls response.json() for every successful response can turn this valid result into a parsing failure. If a particular endpoint is documented to return 204, add a branch that resolves to the appropriate empty value for your application. An endpoint expected to deliver a JSON object still needs a clear error if it returns a truncated body or an unexpected type.

Use the status and the endpoint contract together. A header can tell you what the server claims it sent. Parsing and validating the fields tell you whether the resulting value is usable. Those checks are distinct from the transport question the loopback fixture answers.

Default redirects can hide the first status

The fixture starts at /redirect and ends at /ok. With the default redirect behavior, the response you read is the final 200. The redirected flag and final URL expose that a second hop occurred. Use the final URL if your application needs it for reporting. If your integration depends on handling redirects explicitly, set and test the intended redirect mode instead. Also test a representative staging endpoint when redirects cross origins or carry authentication, since this fixture has neither.

Make timeouts intentional

The slow route waits two seconds before sending a response. The client supplies a 200 msAbortSignal.timeout, so the assertion expects TimeoutError. This margin demonstrates cancellation and is not a production latency target. Use a budget that fits your upstream service and caller. Remember that receiving headers and finishing body consumption are separate events. If an endpoint streams results, add a test where headers arrive and the body then stalls.

This example covers a deadline you specify. If you are building a search interface where the user can replace an in-flight request, read the guide to cancelling stale Fetch requests with AbortController. It covers a different reason to abort and how to keep a late response out of the interface.

Add the checks your staging service actually needs

The fixture prints the HTTP version observed by a plain Node HTTP server. It can tell you that this local exchange used HTTP/1.1. It cannot tell you whether an HTTPS host negotiates HTTP/2, whether a corporate proxy is involved, or whether your deployment trusts the same certificate chain. If those details matter, use a staging endpoint you control, capture its observed protocol and configuration, and keep normal certificate verification enabled.

The Undici 7-to-8 migration guide describes a relevant change. Undici 8 enables HTTP/2 by default when an HTTPS server negotiates it. If your application uses the bundled client with its default dispatcher, compare what your actual staging server reports under both runtimes. An HTTP/1.1 result from this plain loopback fixture cannot answer that question.

Use one integration at a time. For a search API, send a known query that includes a space or a repeated parameter and assert the values the server receives. Our URLSearchParams guide for repeated filters has an example fixture you can adapt. For a JSON API, add a response with a documented missing field. For a file endpoint, check the expected size or checksum after body consumption. Pick cases that match the failure modes your readers or users would notice.

If the loopback check passes under both runtimes but staging fails, compare the code path that runs in staging first. Look for a different dispatcher, proxy setting, dependency lockfile, runtime startup flag, or deployment image. The Node Fetch reference describes how a dispatcher can change how requests are sent. Test one configuration difference at a time to keep the result interpretable.

Example: The API succeeds, but the wrapper still throws

Imagine a settings endpoint that documents a 204 when the requested change succeeds. The loopback output is clean on both versions, yet your staging test reports a JSON parsing error after saving settings. First, record the status and content type immediately before your wrapper decodes the response. A 204 followed by a thrown parsing error points to the decoding step. Trace whether the wrapper reaches response.json() for that endpoint. In this case, add an explicit empty-success branch and assert that calling code handles its return value.

Rerun the staging test with the same request under the old and new runtimes. Also run one endpoint that is contractually required to return a JSON object. The 204 exception should not turn a malformed response from that second endpoint into an apparent success. If the old and new runs disagree before the wrapper starts decoding, keep the upgrade on hold and compare the requests and response metadata in staging. You now have a specific discrepancy to investigate.

Use the comparison to decide whether to roll out

Record the two Node and Undici versions, the commit or dependency lockfile used, and the outcomes that matter. Repeat the test in CI with the runtime you will deploy. If the service has a staging environment, verify one representative success and one realistic failure there before changing its runtime pin. Keep the previous deployment available while you watch request errors, timeouts, and body-parsing failures after rollout.

A matching local result gives you a baseline you can rerun when the next patch arrives. A mismatch gives you a small test case to work on before making broad changes. In either case, you leave the upgrade with a reproducible answer about what the Fetch paths in your own application do.