Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next js 13 server error " TypeError: fetch failed " #54961

Closed
1 task done
AhmedShehata98 opened this issue Sep 4, 2023 · 68 comments
Closed
1 task done

next js 13 server error " TypeError: fetch failed " #54961

AhmedShehata98 opened this issue Sep 4, 2023 · 68 comments
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation please verify canary The issue should be verified against next@canary. It will be closed after 30 days of inactivity

Comments

@AhmedShehata98
Copy link

Link to the code that reproduces this issue or a replay of the bug

https://github.com/AhmedShehata98/shoperz

To Reproduce

  1. Start the application with npm run dev
  2. After many re-render or refresh
  3. Facing 500 Internal Server Error page
  4. in terminal get this error :

TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async invokeRequest (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\server-ipc\invoke-request.js:17:12)
at async invokeRender (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\router-server.js:254:29)
at async handleRequest (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\router-server.js:447:24)
at async requestHandler (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\router-server.js:464:13)
at async Server. (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\start-server.js:117:13) {
cause: Error: connect ECONNREFUSED ::1:58392
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 58392
}
}

Current vs. Expected behavior

i followed the server start instractions by " npm run dev " I expected its works fine without error but got error
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async invokeRequest (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\server-ipc\invoke-request.js:17:12)
at async invokeRender (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\router-server.js:254:29)
at async handleRequest (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\router-server.js:447:24)
at async requestHandler (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\router-server.js:464:13)
at async Server. (D:\my-work\Progo-soft\libya-zon\node_modules\next\dist\server\lib\start-server.js:117:13) {
cause: Error: connect ECONNREFUSED ::1:58392
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 58392
}
}

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 18.17.1
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.19
      eslint-config-next: 13.4.19
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

No response

@AhmedShehata98 AhmedShehata98 added the bug Issue was opened via the bug report template. label Sep 4, 2023
@cosieLq
Copy link

cosieLq commented Sep 4, 2023

Maybe related to #51605

@balazsorban44 balazsorban44 added the please verify canary The issue should be verified against next@canary. It will be closed after 30 days of inactivity label Sep 4, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2023

Please verify that your issue can be recreated with next@canary.

Why was this issue marked with the please verify canary label?

We noticed the provided reproduction was using an older version of Next.js, instead of canary.

The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.

If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.

How can I quickly verify if my issue has been fixed in canary?

The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases. You can also use the GitHub templates (preferred) for App Router and Pages Router, or the CodeSandbox: App Router or CodeSandbox: Pages Router templates to create a reproduction with canary from scratch.

My issue has been open for a long time, why do I need to verify canary now?

Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.

What happens if I don't verify against the canary version of Next.js?

An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without reproduction steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@awe79
Copy link

awe79 commented Sep 4, 2023

This is not a new issue. This has have been happening since roughly 13.2.x However it seems to have gotten worse since 13.4.19. For me on windows, i'm getting now 1 crash per day on my development environment. In the past the next server would restart which was nice, but now i have to manually restart. Also it seems to be less of a problem on mac. If you search there might me some tips in similar topics. In general development on windows with Next 13 is far from optimal

@felri
Copy link

felri commented Sep 4, 2023

I have the same problem, in my case is when I'm sending files from (5 - 20mb) to supabase storage.

const uploadResult = await storageClient
  .from(STORAGE_BUCKET)
  .upload(filePath, file, {
    contentType: `video/${fileExt}`,
    upsert: true
  });
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11522:11) {
  cause: Error: write EPIPE
      at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16)
      at WriteWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -32,
    code: 'EPIPE',
    syscall: 'write'
  }
}

@harrisyn
Copy link

harrisyn commented Sep 4, 2023

Confirming that this has been driving me nuts lately.

really frustrating building in NextJs lately.

@felri
Copy link

felri commented Sep 4, 2023

@balazsorban44 I'm using canary btw

"next": "^13.4.20-canary.15",

@cosieLq
Copy link

cosieLq commented Sep 4, 2023

The problem seems getting worse when NextJs got rid of node-fetch (compiled) and exclusively used undici. It happened since v13.4.x.

What may also be useful is to provide a benchmark for the performance of NextJs API routes. Is there information about for example the rate limit of the API routes? Essentially, API is API.

@balazsorban44
Copy link
Member

Anyone saying "same issue", or not posting helpful comments to debug, please note that adding a reproduction link helps us verify/investigate more than anything. Stating your Next.js version in itself or an out-of-context code snippet is not very helpful. 🙏 Please comment your reproduction repository instead, we would like to investigate!

I added the label please verify canary, because the linked reproduction is on 13.3.1 which closes in on being half a year old version. We've introduced many improvements since then. The reproduction is also pretty complex and is not clear from the reproduction steps how to recreate the issue. @AhmedShehata98 do you have some specific steps you could share?

@cosieLq undici is the de facto fetch implementation that powers Node.js 18+ versions. In those cases, we don't polyfill at all, just rely on the underlying runtime, so in that case this would indicate an upstream bug. Do you have a codebase we could look at to verify what's the issue that you are seeing "getting worse"?

For anyone, upgrading Node.js might be a good start to verify if it's related or not. 👍

@eduardolandaiqmetrix
Copy link

Adding a link to a Git Project which can replicate the same issue, I tested it by using Node 16 and 18, Git Project, It may require some test supabase credentials to work but does the job to replicate the issue. Error:

"TypeError: fetch failed\n    at Object.fetch (node:internal/deps/undici/undici:11576:11)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"

And another medium link where a similar issue has been explained and "tried" to fix
https://medium.com/@kaloyan_17221/fix-vercel-next-js-fetch-failed-from-undici-polyfill-8c66346c9c2f

@cosieLq
Copy link

cosieLq commented Sep 5, 2023

@balazsorban44 I don't have a codebase to prove that it is indeed "getting worse". It's just my guess.

I'm working on a project with NextJs v13.2.4. We were trying to upgrade it to v13.4.x. And then in the production environment, the "TypeError: fetch failed" showed up so often (~1 per minute) that we had to roll it back.

Then after a bit searching, I found these issues: #51605 #53353 , thinking that these issues are somehow related to our problem.

In our case, the error codes differ ("ECONNREFUSED" and "ECONNRESET"). They take place after calling on our NextJs API route.

I'm not sure if it's a (upstream) bug or not. It has something to do with load.
If I call API route at high rate (with curl for example), the "TypeError: fetch failed" logs also appear with NextJs v13.2.4. But in production we haven't seen it so far as often as with v13.4.x.

That's why I'm also curious about the performance of NextJs API routes. Are there guidelines hereof?
Because of these errors, we aren't able to upgrade NextJs and leverage the app dir.

@LufyCZ
Copy link

LufyCZ commented Sep 7, 2023

A reproduction that works consistently for me:

Repo
feature/steer-integration

pnpm i
export NEXT_PUBLIC_POOLS_API_V0_BASE_URL=https://pools-git-feature-steer-integration.sushi.com
pnpm exec turbo run dev --filter=evm

go to http://localhost:3000/pool/137:0x21988c9cfd08db3b5793c2c6782271dc94749251/smart

open the file
sushiswap/apps/evm/ui/pool/Steer/SteerLiquidityDistributionWidget/SteerLiquidityInRangeChip.tsx

change Range on line 47 to Rangea and back to Range a couple times, it happens faster one time and slower the other

Might have something to do with a client component under a server component? Wild guess.

Noticed this line in the log a couple seconds before it started erroring out (both times I tried this repro):
evm:dev: - warn The server is running out of memory, restarting to free up memory.
I've got 16GB of memory.

Finally, the error itself:

evm:dev: TypeError: fetch failed
evm:dev:     at Object.fetch (node:internal/deps/undici/undici:11576:11)
evm:dev:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
evm:dev:     at async invokeRequest (/home/lufy/sushi/sushiswap/node_modules/.pnpm/next@13.4.19_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:17:12)
evm:dev:     at async invokeRender (/home/lufy/sushi/sushiswap/node_modules/.pnpm/next@13.4.19_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/lib/router-server.js:254:29)
evm:dev:     at async handleRequest (/home/lufy/sushi/sushiswap/node_modules/.pnpm/next@13.4.19_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/lib/router-server.js:447:24)
evm:dev:     at async requestHandler (/home/lufy/sushi/sushiswap/node_modules/.pnpm/next@13.4.19_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/lib/router-server.js:464:13)
evm:dev:     at async Server.<anonymous> (/home/lufy/sushi/sushiswap/node_modules/.pnpm/next@13.4.19_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/lib/start-server.js:117:13) {
evm:dev:   cause: Error: connect ECONNREFUSED 127.0.0.1:41375
evm:dev:       at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
evm:dev:     errno: -111,
evm:dev:     code: 'ECONNREFUSED',
evm:dev:     syscall: 'connect',
evm:dev:     address: '127.0.0.1',
evm:dev:     port: 41375
evm:dev:   }
evm:dev: }

@eduardolandaiqmetrix
Copy link

I tried yesterday the last Canary version while using Node 18 and it worked.

@SpBills
Copy link

SpBills commented Sep 8, 2023

For some reason my repository random stalled out and started having this issue on dev. Haven't been able to get it to work since.

@harrisyn
Copy link

harrisyn commented Sep 8, 2023

@balazsorban44 I wish I could provide more details, I basically found it difficult to work in dev mode.
I had upgraded the next version from 13.4.10 to 13.4.19, and would consistently get the above error, and my dev environment became unbearably slow.

I downgraded to 13.4.13 and it's been stable since.

I might test out the canary version once I roll my current implementation out.

hopefully, someone else may have provided more context before then.

@gregawoods
Copy link

Hi,

Just wanted to chime in to say we're experiencing the same symptoms in our application. We recently upgraded next.js from 13.4.7 to 13.4.19. Node version 18.7.1.

We see steady increase in memory usage culminating in a The server is running out of memory, restarting to free up memory. log message. After that the app starts returning an internal server error and logging out the same TypeError: fetch failed stack trace as above over and over. Requires a restart to get the app running again.

I'm going to try rolling back next.js and let it run for a day or two.

@meotimdihia
Copy link

meotimdihia commented Sep 10, 2023

This problem happens with next start on the production. Ubuntu 20.20 Node.js v18.17.1
It happens randomly when our server is busy, once it happens next.js server can't continue to work => We have to restart the Next.js server.

Fastify servers work fine, but it just happens with Next.js servers.

P/S Downgrade to "next": "13.4.12" works fine for me.

@madfcat
Copy link

madfcat commented Sep 10, 2023

Updated from 13.4.10 to 13.4.19 and now get this error

@karlhorky
Copy link
Contributor

@Elvincth
Copy link

This issue continues happened with the latest ^13.4.19 version with my m1 machine in development mode, it happens randomly.

@karlhorky
Copy link
Contributor

For all the people who have been reporting that it happens with Next.js v13.4.19, please upgrade to the latest canary on the Releases page (currently 13.4.20-canary.23) and try again - this will be helpful to see if the problem still occurs

(I am also experiencing this issue on the latest canary personally, but would be good to get some other data points)

@Elvincth
Copy link

Elvincth commented Sep 11, 2023

I recently switched to version 13.4.20-canary.23 and encountered an unexpected type error that read "X TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed". I'm unsure of what caused this error, as there is no backend portion to my next.js project.

image

@LiorDueto
Copy link

LiorDueto commented Sep 11, 2023

cross-posting in case it helps:
We also encountered similar 'out of memory', 'fetch' errors and eventual crashes (13.4.19).
Disabling image optimization solved this for us and server memory in production has become more stable.
Just add images: { unoptimized: true} to next configuration.

Eventually we do want to benefit from image optimization, but at this point the memory footprint is simply too high.

@naolmelesse
Copy link

For all the people who have been reporting that it happens with Next.js v13.4.19, please upgrade to the latest canary on the Releases page (currently 13.4.20-canary.23) and try again - this will be helpful to see if the problem still occurs

(I am also experiencing this issue on the latest canary personally, but would be good to get some other data points)

Hello @karlhorky , I have been facing the same issue with Next.js v13.4.19, (Node.js v18.16.0). it is happening frequently. It restarts in less than 5 minutes of starting the server. I am new to Next.js and How do I upgrade to the canary release?

@Elvincth
Copy link

Elvincth commented Sep 12, 2023

For all the people who have been reporting that it happens with Next.js v13.4.19, please upgrade to the latest canary on the Releases page (currently 13.4.20-canary.23) and try again - this will be helpful to see if the problem still occurs
(I am also experiencing this issue on the latest canary personally, but would be good to get some other data points)

Hello @karlhorky , I have been facing the same issue with Next.js v13.4.19, (Node.js v18.16.0). it is happening frequently. It restarts in less than 5 minutes of starting the server. I am new to Next.js and How do I upgrade to the canary release?

See here for updating to canary release. Or you can directly replace the version of next e.g. "next": "13.4.20-canary.24", in your package.json file then rerun the command npm i to update.

@naolmelesse
Copy link

For all the people who have been reporting that it happens with Next.js v13.4.19, please upgrade to the latest canary on the Releases page (currently 13.4.20-canary.23) and try again - this will be helpful to see if the problem still occurs
(I am also experiencing this issue on the latest canary personally, but would be good to get some other data points)

Hello @karlhorky , I have been facing the same issue with Next.js v13.4.19, (Node.js v18.16.0). it is happening frequently. It restarts in less than 5 minutes of starting the server. I am new to Next.js and How do I upgrade to the canary release?

See here for updating to canary release. Or you can directly replace the version of next e.g. "next": "13.4.20-canary.24", in your package.json file then rerun the command npm i to update.

Okay, Thank you @Elvincth

yeolyi added a commit to wafflestudio/csereal-web that referenced this issue Sep 21, 2023
@pealmeid
Copy link

pealmeid commented Sep 27, 2023

I'm getting this same error after upgrading to 13.5.3 from 13.5.2. I'm getting the following error:

TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: Error: self-signed certificate
at TLSSocket.onConnectSecure (node:_tls_wrap:1600:34)
at TLSSocket.emit (node:events:517:28)
at TLSSocket._finishInit (node:_tls_wrap:1017:8)
at ssl.onhandshakedone (node:_tls_wrap:803:12)
at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
}
}

Our solution proxies calls to an external https API server (that's run locally in the dev environment).

We were getting a similar error in the past and our investigation pointed to extending Node's local certificates by using the NODE_EXTRA_CA_CERTS environment variable with the path to a self-signed certificate. After setting this variable the error stopped happening but now it has returned.

Note: if we downgrade to Next 13.5.2 we don't get any errors. The only difference in package.json between the working and the failing solution is Next's version.

Thanks in advance for any guidance on this.

EDIT: I tested this with next@canary and the error continues to happen. The latest version with which it does not happen is 13.5.2

@GoudekettingRM
Copy link

Also getting this. First on 13.4.19, upgraded to 13.5.3, but still getting it.

The following two errors alternate in my console:

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async imageOptimizer (/Users/.../node_modules/next/dist/server/image-optimizer.js:521:29)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/Users/.../node_modules/next/dist/server/next-server.js:519:61)
    at async /Users/.../node_modules/next/dist/server/response-cache/index.js:102:36 {
  cause: Error: connect ENETUNREACH 64:ff9b::34da:19d3:443
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -51,
    code: 'ENETUNREACH',
    syscall: 'connect',
    address: '64:ff9b::34da:19d3',
    port: 443
  }
}
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async imageOptimizer (/Users/.../node_modules/next/dist/server/image-optimizer.js:521:29)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/Users/.../node_modules/next/dist/server/next-server.js:519:61)
    at async /Users/.../node_modules/next/dist/server/response-cache/index.js:102:36 {
  cause: ConnectTimeoutError: Connect Timeout Error
      at onConnectTimeout (/Users/.../node_modules/next/dist/compiled/undici/index.js:1:92227)
      at /Users/.../node_modules/next/dist/compiled/undici/index.js:1:91719
      at Immediate._onImmediate (/Users/.../node_modules/next/dist/compiled/undici/index.js:1:92109)
      at process.processImmediate (node:internal/timers:476:21)
      at process.callbackTrampoline (node:internal/async_hooks:130:17) {
    code: 'UND_ERR_CONNECT_TIMEOUT'
  }
}

Only on next/image, no errors when just using img tags (obviously ... as those don't use imageOptimizer).

I temporarily resolved it by just skipping the default image loader, but that is not the long term solution. I want the default loader for it's optimisations haha

// next.config.js
...
images: {
    loader: 'custom',
    loaderFile: './bin/imageLoader.js',
},
...

// imageLoader.js
'use client';

export default function imageLoader({ src, width, quality }) {
  if (src.includes('?')) {
    return `${src}&w=${width}&q=${quality || 75}`;
  }

  return `${src}?w=${width}&q=${quality || 75}`;
}

@meotimdihia
Copy link

@GoudekettingRM Downgrade to 13.5.2 => It should be fine.
13.5.3 introduced a new bug with fetch - I think.

@GoudekettingRM
Copy link

@meotimdihia

Thanks for the quick reply, same issues on 13.5.2 unfortunately...

@thany
Copy link

thany commented Sep 29, 2023

Still the same on 13.5.3.

Revert to 13.4.x works around it, but it had a severe bug pertaining to locales that was fixed only a few days ago, iirc.

So, it's a matter of choose the lesser evil, at least for the moment 😟

@joggienl
Copy link

joggienl commented Sep 30, 2023

I noticed the same issue as @pealmeid , I have something in place on my system that sets NODE_EXTRA_CA_CERTS. I need it to be able to use some local https service.

The cause of this issue is this PR: #55775. There you can see that process.env.NODE_EXTRA_CA_CERTS gets set. I think that should not be done.

A simple reproduction is to put an export in your login shell that sets NODE_EXTRA_CA_CERTS. If you use zsh you could add to your ~/.zhsrc a line like:

export NODE_EXTRA_CA_CERTS=/path/to/a/ca/you/want/to/load

If you put a console.log in next.config.js you can see process.env.process.env.NODE_EXTRA_CA_CERTS is empty. If you hack into your node_modules and disable the line that sets process.env.NODE_EXTRA_CA_CERTS it works again as expected.

I don't think it helps or solves the original problem of this issue, perhaps this should be a separate issue.

*edit

There is apparently an experimental way to do https with nextjs locally (--experimental-https), but I suppose that should not override process.env.NODE_EXTRA_CA_CERTS if nothing is passed to fill it with. Especially if you are not using the experimental flag (which I am not).

@rcssdy
Copy link

rcssdy commented Oct 1, 2023

Also following here.. having the same issue as of today, locally. Haven't tried to deploy my project, but have tried all of the workarounds listed here, and still no luck.

@timneutkens
Copy link
Member

Just read through every post. They're all about different issues and most are not caused by Next.js but by application code.

As a first step upgrade to the latest stable version of Next.js, for example what #54961 (comment) and other shared has already been fixed. Though unable to reproduce we were able to just remove that code path altogether so that specifically can't happen anymore.

TLDR: Next.js no longer uses fetch in the hot path while rendering, whereas in 13.4 it did, so this error can't happen in the normal rendering path.

For others I'm seeing a bunch of different issues being confused:

  • next build prerender errors out with fetch failed, usually this means:
    • You're trying to fetch an endpoint that doesn't exist at build time (e.g. pages/api or route handlers), for this case call the code directly instead, it's all in the same Next.js application.
    • Your build system doesn't have access to the API (i.e. there's an IP block or something similar)
    • Your build system doesn't have the right API Keys set up (i.e. you forgot to add an environment variable)
  • Fetch failed at runtime during rendering
    • I.e. when you fetch() in a server component you need to ensure that the url you're fetching is available
    • Your runtime system doesn't have access to the API (i.e. there's an IP block or something similar)
    • Your runtime system doesn't have the right API Keys set up (i.e. you forgot to add an environment variable)
  • Fetch failed in Next.js
    • There is a case where Next.js internals fetch(), in particular what was mentioned above wrt the image optimizer, however the error shown above is caused by an image url being provided that fails to fetch, e.g. because you're offline, it's unreachable and such.

Hope that provides enough context!

@joggienl
Copy link

joggienl commented Oct 2, 2023

Thanks for this explanation @timneutkens.

@thany
Copy link

thany commented Oct 3, 2023

@timneutkens In the end, if all it takes is to downgrade Next.js to 13.4 (down from 13.5), you can't reasonably assume the problem is in application code. In such case, it doesn't matter if the url fetch is trying to access, is blocked or otherwise unavailable. It's demonstrably Next.js just spewing out an error that it didn't before, and is not useful.

Isn't not application code or something getting blocked or whatever else, it is purely Next.js that has put extra unneccesary checks in place for the certificate. And frankly, on a development environment, I don't give 2 tosses about the correctness of certificates. If I could work without https, I would, but when I can't, I just don't care about certificates. I'm on a development machine, and I just need to do my job instead of messing around with certificates that have always worked perfectly fine. I don't think that's all that unreasonable, that a developer just wants to get his/her work done, and not have to deal with certificate bollocks. Therefor it's super unproductive to restrict certificate requirements further than they already were.

That is of course, for my version of this problem. Other versions may enjoy different argumentation.

@GoudekettingRM
Copy link

Thanks for the expansive answer @timneutkens.

Not quite sure about the wrt optimizer though. If I skip it, the images load without error - i.e. the images exist and are reachable. As soon as I use the default optimizer it errors with ENETUNREACH/UND_ERR_CONNECT_TIMEOUT. I understand that that is a message that indicates the optimizer cannot get the image, but seeing that the image is reachable and present, I'm confused as to why not and I'm not convinced it's purely application code error. I've tried a few times and I see that sometimes - after like 5 refreshes - it does load, but not always. If I have some time later today I'll see if I can replicate it in a new project.

@amesas
Copy link

amesas commented Oct 3, 2023

Thanks for the expansive answer @timneutkens.

Not quite sure about the wrt optimizer though. If I skip it, the images load without error - i.e. the images exist and are reachable. As soon as I use the default optimizer it errors with ENETUNREACH/UND_ERR_CONNECT_TIMEOUT. I understand that that is a message that indicates the optimizer cannot get the image, but seeing that the image is reachable and present, I'm confused as to why not and I'm not convinced it's purely application code error. I've tried a few times and I see that sometimes - after like 5 refreshes - it does load, but not always. If I have some time later today I'll see if I can replicate it in a new project.

Same problem here.

It's a production build, running on windows using a corporate proxy.

To setup the proxy we were setting up HTTPS_PROXY env var

As it was not enough, ignore in some internal requests, we use node --require to preload some code.

In this case globalAgent.

It worked ok, until undici got into the scene and needed to use the same preloaded code to setup the proxyagent on the global undici object.

And now, since 13.4.13, we get the ECONN_REFUSED on the image optimizer requests. Just downgrading after 13.4.12, everything works ok.

So it looks like NextJs, has problems with corporate proxies as we always have to look for workarounds

PS: all custom code requests works, just internal requests done by NextJS, fail. So probably our scenario is not on the list by @timneutkens

@owenallenaz
Copy link

@timneutkens You're evaluation on bullet point 3 I think is just wrong. There are numerous comments here and in the previous thread describing this issue, and it isn't coming from our code. I repeat. We never see these fetch failed while we hit our own pages... they appear in the logs later, often after the process has been up for hours. In our specific example I have tried load testing the application for an hour handling hundreds of thousands of requests and there are 0 fetch failed. Yet, our production logs are absolutely full of them, when I'm hitting the same exact URL.

There is some sort of internal mechanic going on which is triggering the fetch failed. It aligns with what many of the commentators here are bringing up, and unfortunately I have zero confidence the Next team even thinks this is an issue.

@timneutkens
Copy link
Member

Like I said most are caused by application code, not all. If you're still running into this after upgrading please provide a full reproduction so that we can investigate it further, unfortunately there is no runnable reproduction provided in this issue so far besides the ones mentioning the stacktrace with code that no longer exists.

As said the majority of comments posted on this issue cannot happen anymore as we got rid of the code path being referenced, it no longer exists in 13.5.

Looking forward to looking at a reproduction.

@harrisyn
Copy link

harrisyn commented Oct 3, 2023

Just read through every post. They're all about different issues and most are not caused by Next.js but by application code.

As a first step upgrade to the latest stable version of Next.js, for example what #54961 (comment) and other shared has already been fixed. Though unable to reproduce we were able to just remove that code path altogether so that specifically can't happen anymore.

TLDR: Next.js no longer uses fetch in the hot path while rendering, whereas in 13.4 it did, so this error can't happen in the normal rendering path.

For others I'm seeing a bunch of different issues being confused:

  • next build prerender errors out with fetch failed, usually this means:

    • You're trying to fetch an endpoint that doesn't exist at build time (e.g. pages/api or route handlers), for this case call the code directly instead, it's all in the same Next.js application.
    • Your build system doesn't have access to the API (i.e. there's an IP block or something similar)
    • Your build system doesn't have the right API Keys set up (i.e. you forgot to add an environment variable)
  • Fetch failed at runtime during rendering

    • I.e. when you fetch() in a server component you need to ensure that the url you're fetching is available
    • Your runtime system doesn't have access to the API (i.e. there's an IP block or something similar)
    • Your runtime system doesn't have the right API Keys set up (i.e. you forgot to add an environment variable)
  • Fetch failed in Next.js

    • There is a case where Next.js internals fetch(), in particular what was mentioned above wrt the image optimizer, however the error shown above is caused by an image url being provided that fails to fetch, e.g. because you're offline, it's unreachable and such.

Hope that provides enough context!

I was hoping there will be a response for mine as well.

the same codebase I have been working on since next 11, now can no longer be upgraded beyond 13.4.12, without either the fetch issues or completely fails to build on 13.5.x

I still have no clue what to change except to downgrade back to 13.4.12

@timneutkens
Copy link
Member

timneutkens commented Oct 3, 2023

@harrisyn others already replied to your case , which is to open a new issue with a reproduction: #54961 (comment)

We're unable to help in the majority of cases where the only information shared is a stack trace, please provide a full repository that we can run to investigate. Adding the label so that the comment is posted that will be useful for you to read.

@timneutkens timneutkens added the please add a complete reproduction The issue lacks information for further investigation label Oct 3, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.

Why was this issue marked with the please add a complete reproduction label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.

Please test your reproduction against the latest version of Next.js (next@canary) to make sure your issue has not already been fixed.

If you cannot create a clean reproduction, another way you can help the maintainers' job is to pinpoint the canary version of next that introduced the issue. Check out our releases, and try to find the first canary release that introduced the issue. This will help us narrow down the scope of the issue, and possibly point to the PR/code change that introduced it. You can install a specific version of next by running npm install next@<version>.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

What happens if I don't provide a sufficient minimal reproduction?

Issues with the please add a complete reproduction label that receives no meaningful activity (e.g. new comments with a reproduction link) are automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without reproduction steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@PeterJSung
Copy link

PeterJSung commented Oct 4, 2023

@timneutkens
hello. Thank you for your reply.
I don't know other users' codes or when the error occurs, but the above error occurs irregularly for us(
Occurs when the server is running for more than a day). This happens just by accessing the K8s liveness probe at a time when no one is accessing the site.

Also, this error occurs starting from a certain version.
Some people say that the error does not occur from version v13.4.9 or lower, but most people say it does not occur version v13.4.12 or lower (the error occurs starting with v13.4.13).

If it occurred starting from a specific version of the same code, it would be difficult to say for sure that it was a problem with the users' application code.

When looking at the differences between versions as shown in the link below, it appears that undici has been applied starting from v13.4.13.
(Issue link v13.4.12...v13.4.13#diff-abd1f3c858290361038c2f039d2ebdd031900f46daec6c4a613450dd8fa21dc3)

Also, it appears that a similar issue has been open in undici for quite a long time ago. (issue link nodejs/undici#1602)

From the user's perspective, there is no way to prepare for the sudden occurrence of an error when the patch version is updated from the semantic version.
If backward compatibility is not properly supported, it appears that at least a change is needed starting from the minor version.

We are sorry that we have difficulty providing you with our product code. Probably, it would be difficult for most companies to provide service codes.

@karlhorky
Copy link
Contributor

karlhorky commented Oct 4, 2023

For our apps the fetch errors were resolved with next@13.4.20-canary.35.

But I realize that we have a non-standard configuration for the Next.js Custom Server hostname option (as shown below, we're using 127.0.0.1 instead of localhost, which can be found in various issues to be causing connection problems).

Can't promise for sure that it will help, but maybe worth a shot to try to configure the hostname (not sure how to configure it if you're not using a Custom Server).

const app = next({
-  hostname: 'localhost',
+  // Use '127.0.0.1' instead of 'localhost' to temporarily work around issues
+  // with ECONNREFUSED (connection refused) errors with high port numbers
+  // https://github.com/vercel/next.js/issues/51684#issuecomment-1609345705
+  //
+  // TODO: Revert to 'localhost' when the issue is resolved https://github.com/vercel/next.js/issues/51684
+  hostname: '127.0.0.1',

Other references, potentially useful:

@DuCanhGH
Copy link
Contributor

DuCanhGH commented Oct 4, 2023

@karlhorky I can't reproduce this issue with the latest Next.js canary.
image
image

Perhaps updating would solve the problem? Seems that this issue was happening because Next.js' IPv6 handling was... weird back then.

Also, just saying, but I don't see how 127.0.0.1 is a non-standard thing :)

@karlhorky
Copy link
Contributor

karlhorky commented Oct 4, 2023

I can't reproduce this issue with the latest Next.js canary

Nice, removing the hostname configuration wasn't working back when #49677 was resolved, but I can confirm that removing hostname or configuring hostname: 'localhost' is now working with our apps (on next@13.5.4) 🙌

I'll keep an eye out for any intermittent fetch crashes in the next days too (related to this current issue), just in case.

@DuCanhGH
Copy link
Contributor

DuCanhGH commented Oct 4, 2023

@karlhorky I'd say that the fix for that issue and some PRs after that screwed up Next.js' IPv6 handling even further, so yeah. Got fixed real good after that though, should work properly starting from 13.5 :) I fixed that IPv6 handling, but there were some issues after I got that in (13.4.16). They got rid of the render workers some time after that, which fixed those issues as well (13.4.20-canary.24).

@balazsorban44
Copy link
Member

Tim has given a thorough explanation and what to provide/check in #54961 (comment)

After that comment, all the following comments were talking about 13.4, while it was pointed out to check with 13.5

The OP's issue has been fixed, as the whole code path of that source of issue has been removed altogether.

There might be mixed problems in this issue (some might be bugs), but those should have a dedicated bug report, with a minimal reproduction that we can investigate! (Keep in mind that TypeError: fetch failed is too generic to call it the "same issue". That's why we are asking for reproductions for those as well.)

We understand if you cannot provide your company's source code, but that's not what we were asking for either, as it might not be the best reproduction anyway. Please read: #54961 (comment)

I will close this issue now, but feel free to open a new one - after testing with the latest version of Next.js - with something that we can investigate, we would like to work on all verified Next.js bugs! 🙏

@vercel vercel locked as resolved and limited conversation to collaborators Oct 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation please verify canary The issue should be verified against next@canary. It will be closed after 30 days of inactivity
Projects
None yet
Development

No branches or pull requests