docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

Avoiding EADDRINUSE with HMR

Open laino opened this issue 2 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

When using the default HMR example EADDRINUSE errors can occur, likely because close() is not awaited.

Maybe the example should look something like this instead:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
    if (module.hot?.data?.closePromise) {
        await module.hot.data.closePromise;
        module.hot.data.closePromise = null;
    }

    const app = await NestFactory.create(AppModule, {
        forceCloseConnections: !!module.hot,
    });

    if (module.hot) {
        module.hot.accept();
        module.hot.dispose((data) => data.closePromise = app.close());
    }

    await app.listen(3000);
}

bootstrap();

Minimum reproduction code

https://docs.nestjs.com/recipes/hot-reload

In which operating systems have you tested?

  • [ ] macOS
  • [ ] Windows
  • [x] Linux

laino avatar Feb 14 '23 20:02 laino

would you like to open a PR at https://github.com/nestjs/docs.nestjs.com

image

micalevisk avatar Feb 14 '23 20:02 micalevisk

Thanks for the pointer. I'll do so once I'm reasonably sure that this fixed the sporadic EADDRINUSE I've been getting.

laino avatar Feb 14 '23 21:02 laino