4
votes

I'm relatively new to NestJs and am wondering how can I access the INestApplication application context created in my server bootstrap from an imported Module or Controller? I'm building a framework on top of NestJS and need access to lists of specific types components/services etc that have been provided by my framework as well as applications written with the framework.

1
> I'm relatively new to NestJs [...] I'm building a framework on top of NestJS --- Are you sure this is a good idea? - Livio Brunner

1 Answers

0
votes
  1. For Modules, you are unable to do that since they will be invoked before the ApplicationContext will get bootstrapped. What might help, depending on your case, are Dynamic Modules.

  2. For Controllers, you can use the ModuleRef, which allows you to dynamically import anything from the ApplicationContext.

Very important to note is the { strict: boolean }-option


onModuleInit() {
  // Will search only inside the ModuleContext
  this.service = this.moduleRef.get(Service);
  // Will search in the whole application
  this.service = this.moduleRef.get(Service, { strict: false });
}