In my current project setup, I have developed a shared config module that incorporates multiple config modules. Each of these config modules exports its own service, and the shared module, in turn, exports all the imported modules. At the moment, my application consists of the app module, which imports the shared config module, and the auth module, which also imports the shared config module.
As part of the project, I am utilizing NestJS microservices and attempting to register the ClientsModule
using the registerAsync
method to access the auth config service in my auth module.
Here is the directory architecture:
/**
config/
- shared-config.module.ts
- app/
- app-config.service.ts
- app-config.module.ts
- auth/
- auth-config.service.ts
- auth-config.module.ts
... other config modules
- auth/
- auth.module.ts
- app/
- app.module.ts
*/
shared-config.module.ts :
@Module({
imports: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */],
export: [AppConfigModule, MicroServiceAuthConfigModule, /* and other modules */]
})
export class SharedConfigModule {}
auth-config.module.ts :
@Module({
imports: [
ConfigModule.forRoot({
... some config
}),
],
providers: [MicroServiceAuthConfigService],
exports: [MicroServiceAuthConfigService],
})
export class MicroServiceAuthConfigModule {}
The issue arises when I try to utilize the MicroServiceAuthConfigService
to create the ClientsModule
within my AuthModule
.
auth.module.ts :
@Module({
imports: [
SharedConfigModule,
ClientsModule.registerAsync([
{
name: 'AUTH_PACKAGE',
inject: [MicroServiceAuthConfigService],
useFactory: (authConfigService: MicroServiceAuthConfigService) => ({
transport: Transport.GRPC,
options: {
url: authConfigService.url,
package: authConfigService.package,
protoPath: authConfigService.protoPath,
},
}),
},
]),
],
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
app.module.ts :
@Module({
imports: [AuthModule, SharedConfigModule],
})
export class AppModule {}
Despite importing the SharedConfigModule, I encounter an error while trying to access the MicroServiceAuthConfigService within the useFactory
. The error message is as follows:
Nest can't resolve dependencies of the AUTH_PACKAGE (?). Please make sure that the argument MicroServiceAuthConfigService at index [0] is available in the ClientsModule context.
Potential solutions:
- If MicroServiceAuthConfigService is a provider, is it part of the current ClientsModule?
- If MicroServiceAuthConfigService is exported from a separate @Module, is that module imported within ClientsModule? @Module({ imports: [ /* the Module containing MicroServiceAuthConfigService */ ] })
It is puzzling as I have successfully injected the SharedConfigModule
in my app module, and when using
app.get(MicroServiceAuthConfigService)
in my main.ts
file, it functions correctly. What might I be doing wrong?