Hiya,
I have a module with the following structure
@Module({
providers: [APIController, MyService],
exports: [APIController],
})
export class APIModule {}
which is then imported in my AppModule
@Module({
imports: [APIModule],
controllers: [APIController],
})
The controller itself looks something like the following
@Controller('api')
export class APIController {
constructor(private readonly myService: MyService) {}
}
I would expect this to work fine, as I don't want MyService to be available to other modules in my application, but I do want it to be provided to APIController. However, unless I also add MyService to my exports, I end up with a dependency resolution failure.
[Nest] 846460 - 09/01/2023, 3:22:28 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the APIController (?). Please make sure that the argument MyService at index [0] is available in the AppModule context.
Potential solutions:
- Is AppModule a valid NestJS module?
- If MyService is a provider, is it part of the current AppModule?
- If MyService is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing MyService */ ]
})
Am I missing something? From my reading of the docs, I'd expect this to work, as I am providing the service to my providers, which should thus be made available to the controller.
https://docs.nestjs.com/modules
providers: the providers that will be instantiated by the Nest injector and that may be shared at least across this module
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).