nx icon indicating copy to clipboard operation
nx copied to clipboard

New NX project - NextJS connect ECONNREFUSED 127.0.0.1:4200

Open binaryartifex opened this issue 1 year ago • 20 comments

Current Behavior

My nextjs applications are no longer being served locally. While waiting for the initial build to view application locally in the browser, after a minute or two I am presented with a "connect ECONNREFUSED" error.

Expected Behavior

To view my application locally without interruption.

GitHub Repo

No response

Steps to Reproduce

  1. pnpm dlx create-nx-workspace --packageManager="pnpm" 1a. React Stack -> NextJs Framework -> Integrated Monorepo -> Yes to App Router -> CSS Stylesheets -> No to cache
  2. Start application, wait several minutes. It will either not appear at all with an eventual ECONNREFUSED error, or it will appear for a few minutes before crashing with the same error.

Nx Report

Node   : 18.15.0
   OS     : win32-x64
   pnpm   : 8.6.2
   
   nx                 : 16.4.2
   @nx/js             : 16.4.2
   @nx/jest           : 16.4.2
   @nx/linter         : 16.4.2
   @nx/workspace      : 16.4.2
   @nx/cypress        : 16.4.2
   @nx/devkit         : 16.4.2
   @nx/eslint-plugin  : 16.4.2
   @nx/next           : 16.4.2
   @nx/react          : 16.4.2
   @nrwl/tao          : 16.4.2
   @nx/web            : 16.4.2
   typescript         : 5.1.6

Failure Logs

> nx run public-website:serve:development

- ready started server on localhost:4200, url: http://localhost:4200
- event compiled client and server successfully in 2s (311 modules)
- wait compiling...
- event compiled client and server successfully in 1104 ms (311 modules)
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^
Error: connect ECONNREFUSED 127.0.0.1:4200
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 4200
}
Node.js v18.15.0

Operating System

  • [ ] macOS
  • [ ] Linux
  • [X] Windows
  • [ ] Other (Please specify)

Additional Information

Updating the next package to the latest one doesn't help either. I suspect the issue lies with NX and not NextJS because after creating an initial application with just nextjs without the use of NX, it appears to works just fine.

binaryartifex avatar Jul 01 '23 08:07 binaryartifex

tried updating to the latest 16.5.0-beta.0 with my production workspace and it still occurs. only this time it will build and serve locally, however in less than 2 minutes it disconnects with the same error.

binaryartifex avatar Jul 01 '23 11:07 binaryartifex

Facing the same issue, does this have anything to do with this merge https://github.com/nrwl/nx/pull/17856 ?

rahulretnan avatar Jul 01 '23 11:07 rahulretnan

I solved the issue by adding, hostname value as 0.0.0.0 in the project.json

"serve": {
      "executor": "@nx/next:server",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "myapp:build",
        "dev": true
      },
      "configurations": {
        "development": {
          "buildTarget": "myapp:build:development",
          "dev": true,
          "hostname": "0.0.0.0"
        },
        "production": {
          "buildTarget": "myapp:build:production",
          "dev": false
        }
      }
    }

rahulretnan avatar Jul 01 '23 11:07 rahulretnan

I just learned that NextJS doesn't even use the React version you install in your NPM lib when using the app directory. Next is the current lovechild of the front-end community, but that lib seriously breaks a lot of expected conventions.

It seems that this hostname fix is more of a hack? It would nice to know what the real issue is under the hood. The last couple NextJS updates have also caused my project to run out of memory every 30m or so (in dev mode)

Brian-McBride avatar Jul 01 '23 19:07 Brian-McBride

This started happening to me after upgrading from 16.4.0 to nx 16.4.2

I run 4 next.js apps locally for dev, all with "nx serve"

I also noticed a huge difference in the memory usage of each node process involved:

  • In 16.4.0, each node process remains between 724 MB and 1005 MB.
  • In 16.4.2, each node process keeps growing until they get over 4400MB and cause my Mac to run out of RAM.

charleskoehl avatar Jul 02 '23 02:07 charleskoehl

I am facing the same issue.

Upgraded from 16.3.2 to 16.4.2 and now I can not serve my next.js application locally anymore.

ubirajaramneto avatar Jul 02 '23 22:07 ubirajaramneto

Same issue here, went from 16.3.2 to 16.4.2 and cannot develop locally on mac:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^
Error: connect ECONNREFUSED 127.0.0.1:4200
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 4200
}
Node.js v18.12.1

MartinDavi avatar Jul 03 '23 14:07 MartinDavi

Got the same issue

ajmalmsali avatar Jul 03 '23 18:07 ajmalmsali

📉 Downgrade Nx version

🔀 Command: nx migrate 16.3.2

kkeertipati avatar Jul 03 '23 20:07 kkeertipati

It might not be related, but I describe a similar memory resource issue here: https://github.com/nrwl/nx/issues/17395

andersonba avatar Jul 04 '23 04:07 andersonba

Have same issue

olegshilov avatar Jul 04 '23 12:07 olegshilov

What version of Next is everyone using? These may be partially related:

  • https://github.com/vercel/next.js/issues/52215
  • https://github.com/vercel/next.js/issues/51684
  • https://github.com/vercel/next.js/pull/51378
  • https://github.com/vercel/next.js/pull/51887

I think the last one is the fix. Using node 16 works for me, but not node >16

michaelangeloio avatar Jul 05 '23 13:07 michaelangeloio

I was and still am on [email protected] and [email protected], before and after upgrading nx and reverting.

MartinDavi avatar Jul 05 '23 13:07 MartinDavi

Can you upgrade to the latest version of Nx? We just published v16.5.0.

Initially, we thought that having the defaulted localhost as the hostname would be fine but there it's actually an issue with Node: https://github.com/nodejs/node/issues/40537. We reverted that change and that has been published

Here is a repo of a fresh app: https://github.com/ndcunningham/issue-17902

ndcunningham avatar Jul 05 '23 15:07 ndcunningham

Can you upgrade to the latest version of Nx? We just publish v16.5.0.

Initially, we thought that having the defaulted localhost as the hostname would be fine but there it's actually an issue with Node: nodejs/node#40537. We reverted that change and that has been published

Here is a repo of a fresh app: https://github.com/ndcunningham/issue-17902

Can you reference the PR associated with the revert?

michaelangeloio avatar Jul 05 '23 15:07 michaelangeloio

I'm on [email protected] and node v18.14.1

charleskoehl avatar Jul 06 '23 03:07 charleskoehl

I upgraded to 16.5.0 and the problem is gone. node processes no longer keep growing.

charleskoehl avatar Jul 06 '23 03:07 charleskoehl

Can you reference the PR associated with the revert?

Here is the PR for the revert: https://github.com/nrwl/nx/pull/17931

ndcunningham avatar Jul 06 '23 14:07 ndcunningham

I am still seeing the same issue even after upgrading to the latest nx version- we are on [email protected] and [email protected]. Falling back to [email protected] also doesn't seem to help. Node version is 18.16.1.

Reverting to using Node 16 seems to fix it.

podjames avatar Jul 06 '23 16:07 podjames

@podjames

I am still seeing the same issue even after upgrading to the latest nx version- we are on [email protected] and [email protected]. Falling back to [email protected] also doesn't seem to help. Node version is 18.16.1.

Reverting to using Node 16 seems to fix it.

You can try nx reset. If it's doesn't not help, try to remove lockfile and reinstall dependencies btw. We use latest NextJS and after update all works as expected

olegshilov avatar Jul 06 '23 16:07 olegshilov

@olegshilov

Thank you - I think we have found our issue now. We had the hostname in our Next.js project.json files set to 'localhost'. Removing this line from the project.json files seemed to fix the problem.

podjames avatar Jul 07 '23 08:07 podjames

Closing as I think the issue has been fixed. Thanks!

ndcunningham avatar Jul 07 '23 14:07 ndcunningham

trigger en la promesa que se hace en js

Paufdez30 avatar Jul 20 '23 23:07 Paufdez30

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

github-actions[bot] avatar Aug 20 '23 00:08 github-actions[bot]