Dynamic ConfigModule cannot Inject in custom JwtModule

1k Views Asked by At

I created my Dynamic configModule to extract environment variables from a different path. It extracts from an yml file. Everything works properly if a add in some module. Here is my ConfigModule:

import { DynamicModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { EnvConfigService } from './env.config.service';

export class EnvConfigModule {
  /**
   * Create an static function to call directly from the class without instantiation
   * @param options: Our config module attributes or properties
   * @returns DynamicModule
   */
  static register(options): DynamicModule {
    return {
      module: ConfigModule,
      providers: [
        {
          provide: 'CONFIG_OPTIONS',
          useValue: options,
        },
        EnvConfigService,
      ],
      exports: [EnvConfigService],
    };
  }
}

Now when I want to add that configuration in the new custom JwtModule, CustomJwtModule:

...
import { EnvConfigModule } from 'src/utils/environment/env.config.module';
import { EnvConfigService } from 'src/utils/environment/env.config.service';

@Module({
  imports: [
    JwtModule.registerAsync({
      imports: [EnvConfigModule],
      inject: [EnvConfigService],
      useFactory: (configService: EnvConfigService) => {
        const base64_pubKey = configService.get(ACCESS_PUBLIC_KEY_NAME);
        return {
          publicKey: Buffer.from(base64_pubKey, KEY_ENCODING),
          signOptions: {
            algorithm: ENCRYPTION_ALGORITHM,
          },
        };
      },
    }),
  ],
  providers: [
    {
      provide: JWT_ACCESS_SERVICE,
      useExisting: JwtService,
    },
  ],
  exports: [JWT_ACCESS_SERVICE],
})
export class JWTAccessModule {}

And here is my error:

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

I guess, I do get that error because it does not inject properly in JWT module my service.

My app.module.ts is implemented in that way

...
import { EnvConfigModule } from './utils/environment/env.config.module';

@Module({
  imports: [
    IamModule,
    EnvConfigModule.register({
      folder: 'config/environment/',
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
1

There are 1 best solutions below

0
On BEST ANSWER

Ok, it seems I was importing wrongly. It was missing some part of the configuration. Here is the working example of CustomJwtModule:

...
import { EnvConfigModule } from 'src/utils/environment/env.config.module';
import { EnvConfigService } from 'src/utils/environment/env.config.service';

@Module({
  imports: [
    JwtModule.registerAsync({
      imports: [EnvConfigModule.register({
         folder: 'config/environment/',
      }),
      ],
      inject: [EnvConfigService],
...