I am studying NestJS and i have one question about the order of initialization in NestJs. When a NestJS application get bootstraped, in what order does the dependencies get created ? Module first or provider first or controller first ?
I try to read NestJs documentation but it wasn't clear enough.
NestJS simply using top-down approach, the explaination is :
When a NestJS application is bootstrapped, it begins by initializing the root module, which then initializes any imported modules in a recursive manner. This means that modules are initialized in a top-down fashion, with the root module being the first to initialize, followed by any imported modules.
Once all the modules are initialized, the next step is to register the providers. Providers are registered within each module, and they can be used across the module hierarchy. Providers can be defined in two ways - either as a class with the @Injectable() decorator or as a value (e.g. string, number) using the @Inject() decorator.
Finally, after all providers have been registered, controllers are initialized. Controllers are responsible for handling incoming requests and returning responses, and they can make use of providers to handle the request logic.
breaking down you can find using NEST_DEBUG=true from your ENV variable
you can hook also such as the onModuleInit() method in modules or the onModuleInit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With