My project has a class structure set up like this:
Inside the libs/childmodule/src/child.module.ts, I have a ChildModule. It is mapped to @app
in the taconfig.json file.
In addition, there is a ParentModule where I am attempting to import the ChildModule. The code structure is as follows:
ChildModule
:
import { Module } from '@nestjs/common';
import { ChildService } from './child.service';
import { LoggerModule } from '@app/logger';
import { CommonModule } from '@app/common';
import { SecretsModule } from '@app/secrets';
@Module({
providers: [ChildService],
exports: [ChildService],
imports: [
LoggerModule,
CommonModule,
SecretsModule,
],
})
export class ChildModule {}
The ParentModule
looks like this:
import { ChildModule } from '@app/child';
@Module({
imports: [SomeModule, LoggerModule, ChildModule],
controllers: [ParentController],
providers: [ParentService],
exports: [ParentService],
})
export class ParentModule {}
Currently, I have not yet used the ChildService
via Dependency Injection (DI).
The error message I am encountering reads:
Error: Nest cannot resolve dependencies of the ChildService (LoggerService, CommonService, ?). Please ensure that the SecretsService at index [2] is available in the ChildModule context.
Possible solutions:
- If SecretsService is a provider, is it included in the current ChildModule?
- If SecretsService is exported from a separate @Module, is that module imported within ChildModule?
@Module({
imports: [ /* the Module containing SecretsService */ ]
})
The way I understand it, if I import a module (such as ChildModule) into the parent module, all the dependencies of ChildModule should be automatically resolved. In other words, I should not have to manually list all of ChildModule's dependencies in the providers of my parent module.
I am at a loss as to what may be missing here.
For further clarification, here is the SecretsModule
:
SecretsModule
import { Global, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SecretsService } from './secrets.service';
import { Module1Module, Module1Service } from '@app/module1';
@Global()
@Module({})
export class SecretsModule {
static forRoot(some_key) {
return {
module: SecretsModule,
imports: [Module1Module, ConfigModule],
providers: [
{
provide: SecretsService,
useFactory: async (
configService: ConfigService,
service1: Module1Service,
) => {
const secretsService = new SecretsService(
configService,
service1,
some_key,
);
await secretsService.init();
return secretsService;
},
inject: [ConfigService, Module1Service],
},
],
exports: [SecretsService],
};
}
}