The context is that I have created a shared config module that imports multiple config modules, each config modules are exporting their own service, the shared module exports each imported module, for the moment in my app i have the app module that import the shared config module and a auth module that imports the shared config module.

In the project i'm using nestjs microservices and i'm trying to register the ClientsModule with registerAsync method to access the auth config service in my auth module.

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 problem is that i'm trying to use the MicroServiceAuthConfigService to create the ClientsModule in 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 {}

Because i have imported the SharedConfigModule, i should access to the MicroServiceAuthConfigService in the useFactory but instead i have the following error:

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 */ ] })

The strange thing is that in my app module i have injected the SharedConfigModule and in my file main.ts i'm using app.get(MicroServiceAuthConfigService) and it's working.

So what am i doing wrong ?

1

There are 1 answers

1
Jay McDoniel On BEST ANSWER

In your ClientsModule.registerAsync you need to add the imports that has the array containing the the module that exports the MicroServiceAuthConfigService provider. Looks like you specifically need

@Module({
  imports: [
    SharedConfigModule,
    ClientsModule.registerAsync([
      {
        name: 'AUTH_PACKAGE',
        imports: [MicroServiceAuthConfigModule],
        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 {}