I have a basic nest app with following code
async function bootstrap(port: number) {
const app = await NestFactory.create(AppModule);
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.TCP,
options: {
port,
},
});
await app.startAllMicroservices();
app.enableCors();
await app.listen(port, () => {
logger.log(`HTTP Server listening at ${port}`);
});
}
const port = process.env?.PORT || 3001;
bootstrap(port);
This code throws error
Error: listen EADDRINUSE: address already in use :::3000
I have basic understanding what is happening, I am using same port for microservice as well as app.listen()
. Even though they are pointing to same port, This should work according to nestjs doc, and this answer https://stackoverflow.com/a/64959626/550907
But I am getting the port already used error in my ubuntu server. When I run it in my local server it works. This makes me thinking perhaps something differently setup in the server.
Wondering if anyone else had the same issue and know how to solve this?
My Observation:
When I comment out the line await app.startAllMicroservices();
the application runs without an error.
So, Microservice starts on the given port
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [NestMicroservice] Nest microservice successfully started 8ms
after that when app tries to start it fails.
Update 2
When I said it was running in my local machine. I didn't mention i was using docker in my local environment.
So, when I am running this in Docker (alpine node) it works, but not when running directly in ubuntu. I've tried it without docker in my local and it didn't work.
Update
Is port 3000 free?
Yes lsof -i tcp:3000
show empty output nothing running on the port.
Did you try changing port?
Yes, it didn't help
Did you try running it using npm instead of yarn?
Yes, it didn't help
Can you provide full console output?
Here is the console when I try to run in dev mode (yarn start creates same issue).
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [NestFactory] Starting Nest application...
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] PrismaModule dependencies initialized 46ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] ClientsModule dependencies initialized 1ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] DiscoveryModule dependencies initialized 1ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] ConfigHostModule dependencies initialized 0ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] AppModule dependencies initialized 1ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] ConfigModule dependencies initialized 0ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] ScheduleModule dependencies initialized 0ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] ConfigModule dependencies initialized 1ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] CacheModule dependencies initialized 15ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [InstanceLoader] CongestionsModule dependencies initialized 6ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [NestMicroservice] Nest microservice successfully started 8ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [RoutesResolver] AppController {/}: 48ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [RouterExplorer] Mapped {/, GET} route 2ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [RoutesResolver] CongestionsController {/congestions}: 0ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [RouterExplorer] Mapped {/congestions, GET} route 1ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [RouterExplorer] Mapped {/congestions/clinic/:id, GET} route 0ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM LOG [NestApplication] Nest application successfully started 162ms
[Nest] 2544 - 05/24/2022, 2:07:03 AM ERROR [NestApplication] Error: listen EADDRINUSE: address already in use :::3000 1ms
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 (/var/www/analyzer/node_modules/@nestjs/platform-express/adapters/express-adapter.js:63:32)
at /var/www/analyzer/node_modules/@nestjs/core/nest-application.js:167:30
at new Promise (<anonymous>)
at NestApplication.listen (/var/www/analyzer/node_modules/@nestjs/core/nest-application.js:156:16)
at bootstrap (/var/www/analyzer/src/main.ts:20:2)
CodePudding user response:
To determine what is using that port:
lsof -i tcp:3000
CodePudding user response:
You have two apps listening on port 3000
- The microservice you connect to your app
- The app itself
You can use a different port for the microservice. As in the snippet below:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.TCP,
options: {
port: 8877
}
})
app.startAllMicroservices();
app.listen(3000);
}
bootstrap();