3

In a NestJs project, I created a module that uses, DiscoveryService in the services. It working fine. I wanted to create a nest library from that module. I followed this nestJs documentation then I am getting the below error. I tried both adding ModulesContainer in to my module imports and providers. nothing helped.

[Nest] 65775 - 10/12/2022, 11:55:03 AM ERROR [ExceptionHandler] Nest can't resolve dependencies of the DiscoveryService (?). Please make sure that the argument ModulesContainer at index [0] is available in the DiscoveryModule context.

Error log

Potential solutions:

  • If ModulesContainer is a provider, is it part of the current DiscoveryModule?
  • If ModulesContainer is exported from a separate @Module, is that module imported within DiscoveryModule? @Module({ imports: [ /* the Module containing ModulesContainer */ ] })

Error: Nest can't resolve dependencies of the DiscoveryService (?). Please make sure that the argument ModulesContainer at index [0] is available in the DiscoveryModule context.

library

    import { DiscoveryService } from '@nestjs/core';
    @Injectable()
    export class EventPublishingService {
      private readonly logger = new Logger(EventPublishingService.name);
      private readonly publishingEventProviders;
    
      constructor(
        private discoveryService: DiscoveryService
      ) {
        this.publishingEventProviders = this.discoveryService
          .getProviders()
          .filter(
            ({ metatype }) =>
              metatype && Reflect.getMetadata(PUBLISHABLE_METADATA, metatype),
          );
      }
    }


    @Global()
    @Module({
      imports: [DiscoveryModule, HttpModule, ConfigModule],
      providers: [
        EventRegistrationService,
        EventManagerServiceApi,
        EventPublishingService],
      exports: [EventPublishingService],
    })
    export class EventManagerModule {}






app module


    @Module({
      imports: [
        ConfigModule.forRoot({
          isGlobal: true,
          cache: true,
          load: [configuration],
        }),
        ScheduleModule.forRoot(),
        MongooseModule.forRootAsync({
          imports: [DataVaultManagerModule],
          inject: [DataVaultManager],
          useClass: MongooseConfigService,
        }),
        ParticipantsModule,
        StudiesModule,
        HealthModule,
        EventManagerModule,
        UsersModule,
      ],
      controllers: [AppController],
      providers: [
        AppService,
        {
          provide: APP_PIPE,
          useClass: ZodValidationPipe,
        },
      ],
    })
    export class AppModule {}

Chathura J
  • 31
  • 1

1 Answers1

0

Try changing constructor from EventPublishingService class like below:

constructor(
  @Inject(DiscoveryService) private discoveryService: DiscoveryService
)
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
pravin deore
  • 91
  • 2
  • 10