next.js: ECONNREFUSED when starting vanilla installation

Verify canary release

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

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP Fri Mar 17 01:52:38 EDT 2023
    Binaries:
      Node: 18.14.2
      npm: 9.6.6
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.2-canary.5
      eslint-config-next: 13.4.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

create-next-app@latest

To Reproduce

npx create-next-app@latest npm run dev

-Error occurs when attempting to navigate to site -No additional packages installed

Describe the Bug

Error: connect ECONNREFUSED 127.0.0.1:35391 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) { errno: -111, code: ‘ECONNREFUSED’, syscall: ‘connect’, address: ‘127.0.0.1’, port: 35391 }

error Error: connect ECONNREFUSED 127.0.0.1:36639 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) { errno: -111, code: ‘ECONNREFUSED’, syscall: ‘connect’, address: ‘127.0.0.1’, port: 36639 } Notice that the port changes everytime the server is started up. I can bring it up and down 100 times and itll likely be on a different port every time.

Every time the server is started up it results in the above error. I went did incremental package updates to pinpoint exactly when it broke. It appears that at 13.3.0 is when it first starts occuring. I have only seen one other thread with the same issue and there was no solution provided (or any answers on SO)

I do realize this is potentially a configuration issue with the server, I have attempted to turn off the firewall completely and same issue occurs. Some guidance on where to check would be appreciated. I see no errors in my nginx logging either.

Expected Behavior

Site should start as expected.

Which browser are you using? (if relevant)

Chrome/Edge/Firefox

How are you deploying your application? (if relevant)

Linux Rhel 8

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 17
  • Comments: 54 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Maybe we can get some attention from @ijjk, @shuding, @huozhi, or @timneutkens 🙏🏻

Experiencing same issue, and it seems to be sending multiple requests until the socket hangs up, not 100% sure but it happens in my project, using sentry for tracking and it’s killing me, too many same errors.

@shehi feel free to open an new issue, make sure there is a reproduction included, thanks!

This issue should never have been closed in the first place and should thus be reopened.

Same issue on my end

Наve the same problem here. NextJS ver 13.4.4 binds three extra ports and has a lot of connections to itself, getting sometimes ECONNREFUSED error image

NextJS ver 13.2.4 has only single binded port image

Moreover, seems to be it leads to memory leak (could be unrelated to this particular issue) image

Getting the same error with a fresh install of Next.js in this application running Next.js v13.4.4, Node.js 18, on Kubernetes.

I am having the same issue on 13.4.7 with Node.js 18.16.1 on macOS.

Anyone who is running into ECONNREFUSED errors could also take a look at this issue:

Specifically, the suggestion from @greifmatthias to switch hostname from 'localhost' to '127.0.0.1' - this worked for our custom Next.js server on next@13.4.7

Downgrading Next.js solved the issue, this is definitely something odd going on in the latest release.

I’m facing the same problem here. The application works in a local container, but not in an external production server (shows this ECONNREFUSED above). Downgrading to 13.2.4 worked.

Issue also happens in AWS ECS docker containers.

Receiving Error: socket hang up ECONNRESET and request is timing out after updating Next.js to 13.4.2

Works fine in Next.js 13.2.4

Issue happens with both Node.js 16.19 and 18.16.

Hi, this has been updated in v13.4.4-canary.10 of Next.js, please update and give it a try!

Nope, still the same.

I can’t reproduce locally on my M1 Mac on Ventura 13.3.1 (22E772610a) but am getting spammed with errors (multiple times a minute) when deployed to Node 19 Alpine instance on Kubernetes. Is anybody getting errors like this?

{“log”:“Error: read ECONNRESET\n at TCP.onStreamRead (node:internal/stream_base_commons:217:20) {\n errno: -104,\n code: ‘ECONNRESET’,\n syscall: ‘read’\n”,“stream”:“stderr”}

{“log”:“Error: socket hang up\n at connResetException (node:internal/errors:717:14)\n at Socket.socketOnEnd (node:_http_client:519:23)\n at Socket.emit (node:events:525:35)\n at endReadableNT (node:internal/streams/readable:1359:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {\n code: ‘ECONNRESET’\n”,“stream”:“stderr”}

I am on Next 13.4.2.

@ijjk , since this problem still persists in v13.4.7, should this task be reopened? Or where can we track for the solution of this problem? Of course there is also #51684 which reports the same.

Wanted to add that this is happening with the pages router too, not just the vanilla (I assume app router) installation.

I still have issues with ECONNREFUSED when using the canary release. Within docker compose setup next.js still isn’t able to properly resolve other services via the service name, all the while nslookup yields the correct results in the containers. I really don’t understand why this issue is closed.

I was able to boot: the core issue was allocatable RAM. At 128MB, those extra web servers needed at boot were not able to be created and so connections were not resolving. 256MB seems to be the bare minimum for me. I think we miss some error messages to give us some visibility on this: it was not easy to understand.

@iicdii just tested. The error is still there.

I updated my old project to the latest NextJS version and deleted every single line of code one by one, remove every file, until I found the issue. It was this line:

const dns = require('dns')
dns.setDefaultResultOrder('ipv4first')

It works with NextJS 13.2.4, but not with the newer versions

Encountered this exact issue too. Removing those lines in my next.config.js fixed it for me when running v13.4.4. I setup a repro here first thinking it was due to Nx but I isolated it to being Next.js. Previous version where it worked for me was v13.3.4.

Note that it also is due to the change in Node.js switching to essentially prefer IPv6 as of v17.0.0. This is mostly an issue when running locally and having (by default) localhost defined as both ::1 and 127.0.0.1 in the /etc/hosts file.

Workarounds include:

  1. Set a custom domain in the hosts file, e.g.:

    127.0.0.1 my-web-app.test
    

    And then use http://my-web-app.test:3000 instead of http://localhost:3000.

  2. When running locally, bind other services to ::1 so that IPv6 lookups work as expected

  3. (not recommended) Remove the ::1 localhost line in /etc/hosts to force it to use 127.0.0.1


Definitely possible there are other issues causing this error though.

13.4.3 has same problem. Thinking about migrating to something else but it’s a lot of work.