nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.92k stars 7.55k forks source link

Error: listen EADDRINUSE: address already in use :::3000 #10205

Closed isnolan closed 2 years ago

isnolan commented 2 years ago

Is there an existing issue for this?

Current behavior

I encountered the problem that 3000 ports were occupied in the project, and the local development environment was normal. In 'sample/03-microservices', switch the port number to 3000 and upload the code to k8s to minimize the recurrence of this problem.

`

nest-typescript-starter@1.0.0 start:prod node dist/main [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [NestFactory] Starting Nest application... [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [InstanceLoader] AppModule dependencies initialized +24ms [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [InstanceLoader] ClientsModule dependencies initialized +0ms [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [InstanceLoader] MathModule dependencies initialized +1ms [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [NestMicroservice] Nest microservice successfully started +7ms [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [RoutesResolver] MathController {/}: +2ms [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [RouterExplorer] Mapped {/, GET} route +1ms [Nest] 17 - 08/31/2022, 10:17:20 AM LOG [NestApplication] Nest application successfully started +2ms [Nest] 17 - 08/31/2022, 10:17:20 AM ERROR [NestApplication] Error: listen EADDRINUSE: address already in use :::3000 +0ms node:internal/errors:465 ErrorCaptureStackTrace(err); ^ Error: listen EADDRINUSE: address already in use :::3000 at Server.setupListenHandle [as _listen2] (node:net:1372:16) at listenInCluster (node:net:1420:12) at Server.listen (node:net:1508:7) at ExpressAdapter.listen (/app/node_modules/@nestjs/platform-express/adapters/express-adapter.js:68:32) at /app/node_modules/@nestjs/core/nest-application.js:166:30 at new Promise () at NestApplication.listen (/app/node_modules/@nestjs/core/nest-application.js:155:16) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async bootstrap (/app/dist/main.js:13:5) { code: 'EADDRINUSE', errno: -98, syscall: 'listen', address: '::', port: 3000 } `

Minimum reproduction code

https://github.com/yhostc/nestjs-03-microservices

Steps to reproduce

  1. use gitlab-ci to build the code.
  2. build docker image.
  3. depoly the code to k8s.

Expected behavior

Successfully used 3000 port.

Package

Other package

No response

NestJS version

^9.0.1

Packages versions

[Nest CLI]
Nest CLI Version : 9.0.0 

[Nest Platform Information]
platform-express version : 9.0.1
microservices version    : 9.0.1
schematics version       : 9.0.1
testing version          : 9.0.1
common version           : 9.0.1
core version             : 9.0.1
cli version              : 9.0.0

Node.js version

16.16.0-alpine

In which operating systems have you tested?

Other

No response

micalevisk commented 2 years ago

Looks like an issue with your environment. Please, use our Discord for such issues. We are using GitHub to track bugs, feature requests, and potential improvements.

isnolan commented 2 years ago

@micalevisk I couldn't think it was my environmental problem, so I tried more.

  1. Build docker locally, the problem remains. In the next few steps, I will try to reproduce this problem with the local docker image

  2. Using other ports, everything works normally. (eliminate environmental factors?)

  3. Disable the code await app.startAllMicroservices(), everything works normally. (seems to be here?)

    Next, read the source code and try to reproduce the problem

  4. Change the TCP_DEFAULT_PORT 3000 port number in node_modules/@nestjs/microservices/constants.js to 3001, everything is back to normal. At this time, the service is still running on port 3000. It is found that ports 3000 and 3001 are occupied.

  5. Explore the code TCP_DEFAULT_PORT and find that it seems that the port number can be customized. Try to define the port number as 3001 in Transport.TCP options. At this time, the service can operate normally on port 3000.

const app = await NestFactory.create(AppModule); app.connectMicroservice({ transport: Transport.TCP, options: { retryAttempts: 5, retryDelay: 3000, port:3001 }, });

await app.startAllMicroservices(); await app.listen(3000);

  1. Change the two ports to 3001, the local development and production modes operate normally, and the copying code to other machines runs normally(not in docker). However, the problem remains in docker.

Here, we find that under the local development and production modes, the two ports can be the same. I believe @nestjs does not intend to make them different. However, we have reason to believe that in the docker environment, there is indeed a possibility of port conflict. My knowledge can not explain how the docker environment will be different. Let's record it for now. I hope someone can explain this. I will avoid this problem by changing the port number for the time being. thank you.