06 / 12

How does the NestJS bootstrap sequence work?

NestJS bootstrap: NestFactory.create() scans the module graph, builds the DI container, instantiates all singletons, fires OnModuleInit hooks (depth-first), fires OnApplicationBootstrap hooks, then starts the HTTP server. On shutdown: OnModuleDestroy, BeforeApplicationShutdown, then OnApplicationShutdown fire in order.

Full bootstrap sequence:
  1. 1
    1. NestFactory.create(AppModule) is called.
  2. 2
    1. NestJS recursively scans the module graph — resolves all imports, providers, exports.
  3. 3
    1. The IoC container is built; all singleton providers are instantiated.
  4. 4
    1. OnModuleInit lifecycle hook fires for each module (depth-first order).
  5. 5
    1. OnApplicationBootstrap fires for each module.
  6. 6
    1. app.listen() starts the HTTP server — app is now ready to accept requests.
Implementing lifecycle hooks