Nestjs dependency injection

DI is a key paradigm of nestjs. Here's some  experience I got after nestjs experiment.

  • Provider are scoped within a module. If you want a provider available everywhere, you need to attach it to a global module using @Global.
  • If a non-global provider is imported into multiple modules, it's initialized multiple times. Global module's providers are initialized only once.
  • Providers can use injected providers of their own using @Inject.
  • Providers by default are useClass. Other options are useFactory, useValue, useExisting
  • Providers can be async if useFactory returns a promise. Then application's startup will be delayed till async tasks are finished.