0
0
NestJSframework~10 mins

Application lifecycle in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Application lifecycle
Start Application
Module Initialization
Dependency Injection Setup
OnModuleInit Hook
Application Ready
Handle Requests
OnApplicationShutdown Hook
Cleanup Resources
Application Stops
This flow shows how a NestJS app starts, initializes modules, sets up dependencies, runs lifecycle hooks, handles requests, and cleans up on shutdown.
Execution Sample
NestJS
import { Injectable, OnModuleInit, OnApplicationShutdown } from '@nestjs/common';

@Injectable()
export class AppService implements OnModuleInit, OnApplicationShutdown {
  onModuleInit() { console.log('Module initialized'); }
  onApplicationShutdown() { console.log('Application shutting down'); }
}
This code defines a service that logs messages when the module initializes and when the app shuts down.
Execution Table
StepActionHook CalledConsole OutputState
1Start ApplicationNoneApp starting
2Initialize ModulesNoneModules initialized
3Setup Dependency InjectionNoneDependencies injected
4Call onModuleInitonModuleInitModule initializedModule ready
5Application ReadyNoneApp ready to handle requests
6Handle RequestsNoneServing requests
7Trigger ShutdownonApplicationShutdownApplication shutting downCleaning up
8Cleanup ResourcesNoneResources cleaned
9Stop ApplicationNoneApp stopped
💡 Application stops after cleanup and shutdown hooks complete.
Variable Tracker
VariableStartAfter Step 4After Step 7Final
appStatestartingmoduleReadyshuttingDownstopped
consoleOutputModule initializedApplication shutting downApplication shutting down
Key Moments - 2 Insights
Why does onModuleInit run before the app is ready to handle requests?
Because onModuleInit is called right after modules and dependencies are set up (see step 4 in execution_table), ensuring the module is ready before serving requests.
When exactly does onApplicationShutdown run?
It runs during the shutdown process (step 7), before the app fully stops, to allow cleanup of resources.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the appState after step 4?
AmoduleReady
Bstarting
CshuttingDown
Dstopped
💡 Hint
Check variable_tracker row for appState after step 4.
At which step does the application start handling requests?
AStep 3
BStep 5
CStep 7
DStep 9
💡 Hint
See execution_table row where 'Application Ready' and 'Serving requests' state appear.
If onModuleInit was removed, what would change in the execution flow?
AModule initialization would be skipped
BApplication would not start
CNo console output at step 4
DShutdown hook would not run
💡 Hint
Look at consoleOutput in execution_table at step 4.
Concept Snapshot
NestJS Application Lifecycle:
- Start app -> initialize modules
- Setup dependencies
- onModuleInit runs after DI setup
- App ready to handle requests
- onApplicationShutdown runs on shutdown
- Cleanup and stop app
Use lifecycle hooks to run code at these stages.
Full Transcript
The NestJS application lifecycle begins when the app starts. Modules initialize and dependencies are injected. Then, the onModuleInit hook runs to signal module readiness. After that, the app is ready to handle requests. When the app shuts down, the onApplicationShutdown hook runs to clean up resources before the app stops. This lifecycle helps organize code to run at the right time during app start and stop.