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.91k stars 7.55k forks source link

Devtools with microservices: Error: listen EADDRINUSE: address already in use :::8000 #13486

Closed sjacq closed 5 months ago

sjacq commented 5 months ago

Is there an existing issue for this?

Current behavior

When an app is set to serve as a microservice as well, Devtools tries to allocate / use the port twice, preventing the app from starting.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-gzpmcr?file=src%2Fmain.ts

Steps to reproduce

Display the browser console and start the StackBlitz demo.

If you replace the return 8000 by a random integer (eg between 8000 and 8999) you'll see the 2 ports allocated.

Expected behavior

Devtools should start only once for the NestJS App.

Package

Other package

NestJS Devtools

NestJS version

10.3.8

Packages versions

"@nestjs/common": "^10.3.8",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.3.8",
"@nestjs/devtools-integration": "^0.1.6",
"@nestjs/microservices": "^10.3.8",
"@nestjs/platform-express": "^10.3.8",

Node.js version

21.7.3

In which operating systems have you tested?

Other

No response

micalevisk commented 5 months ago

I guess it's up to you to change the default port (8000), as you've done here:

image

sjacq commented 5 months ago

I guess that's up to you to change the default port (8000), as you did already here

image

Even if you change the port, it will still try to allocate it twice.

Obvisouly, we'd like only one port, so we know which one and can use it in the devtools portal.

micalevisk commented 5 months ago

got it

not sure what is the desired nor the best behavior but I see that that happens because you're using the same root module (AppModule) twice, and so the dev tools module

image


Devtools should start only once for the NestJS App.

I believe that we shouldn't follow this rule because you can have one devtools module for each app just fine. Also, those nestjs apps created by the factory are decoupled. So initializing the devtools module once doesn't feels right

sjacq commented 5 months ago

My bad ! Stupid mistake.

Trying to set up a hybrid application, so microservice should have been defined using app.connectMicroservice({}) and not createMicroservice, which as you pointed out injects twice the app.module.ts

Sorry about that. Case closed.

Thanks for the reactivity.