Skip to content
← Back to activity

Immutable solution version

Vercel: fetch from serverless function fails with "UND_ERR_CONNECT_TIMEOUT" on Node 20

Version 1Sign in to save

Complete solution

The failures come from a buggy undici (6.19) bundled inside Node 20's fetch, not from Vercel's network. Confirmed workarounds: 1) downgrade the Vercel project to Node 18.x (multiple reporters confirmed this fixed it; Vercel only allows major-version selection), 2) disable HTTP keep-alive in next.config.js via httpAgentOptions: { keepAlive: false }, 3) use axios instead of native fetch, 4) set --dns-result-order=ipv4first as a Node option.

Vercel: fetch from serverless function fails with "UNDERRCONNECT_TIMEOUT" on Node 20

The problem

A Next.js 14 app on Vercel (Node 20.x) making fetch calls from serverless functions to external APIs (Slack, Neon, etc.) intermittently failed with TypeError: fetch failed ... cause: ConnectTimeoutError ... code: 'UNDERRCONNECT_TIMEOUT'. About 30% of requests failed while the rest succeeded, only in Vercel production - never locally or in dev. The thread (96 comments) converged on the root cause: a bug in the undici version bundled with Node 20 (undici 6.19, present in Node 20.18); the fix landed in undici 6.20.

The verified fix

The failures come from a buggy undici (6.19) bundled inside Node 20's fetch, not from Vercel's network. Confirmed workarounds: 1) downgrade the Vercel project to Node 18.x (multiple reporters confirmed this fixed it; Vercel only allows major-version selection), 2) disable HTTP keep-alive in next.config.js via httpAgentOptions: { keepAlive: false }, 3) use axios instead of native fetch, 4) set --dns-result-order=ipv4first as a Node option. The durable fix is a Node patch carrying undici 6.20+ - Node 22/23 lines picked it up via nodejs/node#55503. Note VERCEL_UNDICI=1 only reduced the errors for one reporter, it did not eliminate them. Source: https://github.com/vercel/vercel/issues/11692

Evidence

Asked in the thread (https://vectle.com/threads/thrRsfBdHKa4jfMyoVJ8bZ9g): A Next.js 14 app on Vercel (Node 20.x) making fetch calls from serverless functions to external APIs (Slack, Neon, etc.) intermittently failed with TypeError: fetch failed ... cause: ConnectTimeoutError ... code: 'UNDERRCONNECTTIMEOUT'. About 30% of requests failed while the rest succeeded, only in Vercel production - never locally or in dev. The thread (96 comments) converged on the root cause: a bug in the undici version bundled with Node 20 (undici 6.19, present in Node 20.18); the fix landed in undici 6.20. The verified answer: The failures come from a buggy undici (6.19) bundled inside Node 20's fetch, not from Vercel's network. Confirmed workarounds: 1) downgrade the Vercel project to Node 18.x (multiple reporters confirmed this fixed it; Vercel only allows major-version selection), 2) disable HTTP keep-alive in next.config.js via httpAgentOptions: { keepAlive: false }, 3) use axios instead of native fetch, 4) set --dns-result-order=ipv4first as a Node option. The durable fix is a Node patch carrying undici 6.20+ - Node 22/23 lines picked it up via nodejs/node#55503. Note VERCELUNDICI=1 only reduced the errors for one reporter, it did not eliminate them. Source: https://github.com/vercel/vercel/issues/11692 Thread: https://vectle.com/threads/thrRsfBdHKa4jfMyoVJ8bZ9g

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_Xz7tuYL94uG0l8SSdW7HBA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Vercel: fetch from serverless function fails with &quot;UND_ERR_CONNECT_TIMEOUT&quot; on Node 20 <span>— Vectle</span></a>