#Dynamic module with ConfigurableModuleClass : cant inject MODULE_OPTIONS_TOKEN

10 messages · Page 1 of 1 (latest)

polar spade
#

Hello
I'm trying to make a dynamic module using ConfigurableModuleClass but i'm getting the classic error : "Nest can't resolve dependencies of the RedisService (?). Please make sure that the argument "REDIS_MODULE_OPTIONS" at index [0] is available in the RedisModule context"
I have nearly the same as the doc : https://docs.nestjs.com/fundamentals/dynamic-modules#configurable-module-builder

redis.module-definition.ts :
export const { ConfigurableModuleClass: RedisConfigurableModuleClass, MODULE_OPTIONS_TOKEN: REDIS_MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder<RedisOptions['options']>({ moduleName: 'Redis' }) .setClassMethodName('forRoot').build()

redis.module.ts :
@Module({ providers: [RedisService], exports: [RedisService] }) export class RedisModule extends RedisConfigurableModuleClass {}

I instanciate the redis module in my main module like this :
RedisModule.forRootAsync({ inject: [ConfigService], useFactory: async (configService: ConfigService): Promise<RedisOptions['options']> => { return configService.getRedisOptions() } }),

And in my service I'm trying to do this :
`@Injectable()
export class RedisService {
readonly redis: ClientProxy

constructor(@Inject(REDIS_MODULE_OPTIONS_TOKEN) readonly options: RedisOptions['options']) {
console.log(options)
this.redis = ClientProxyFactory.create({
transport: Transport.REDIS,
options
})
}`

Any ideas where I missed something ?

exotic estuary
#

Usually I'll see this error when RedisModule is imported without the forRoot/forRootAsync in some module. Is that the case here?

polar spade
#

I have the RedisModule.forRootAsync() in my MainModule (MainModule also imports JobModule)
Then in JobModule I reimport the RedisModule to get the RedisService

If my understanding is correct it should work no ?
If I move the RedisModule.forRootAsync()inside the JobModule then it work but I would like to configure everything in my MainModule

exotic estuary
#

If my understanding is correct it should work no ?
Nope. Importing the RedisModule without the forRootAsync will not have Nest take into account the initial call of the forRootAsync in the MainModule, because the JobModule needs to be created in order to create the MainModule, and because that's just how the module system works. I would suggest writing a wrapper module around your RedisModule that imports the forRootAsync and exports the module so you can import the wrapper and be done with it

polar spade
#

Do you have an example to do that ?

exotic estuary
#
@Module({
  imports: [RedisModule.forRootAsync(options)],
  exports: [RedisModule],
})
export class RedisWrapperModule {}
#

Now use RedisWrapperModule in the improts instead of RedisModule and you're golden

polar spade
#

Ah look too easy to be true 😅

#

Ah Jesus it finally works, thanks I spend half my afternoon on this and it was that easy 👌

prime coralBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution, if you are having the same issue. If you were the original author of the post and the issue is still fresh (within a few days) and you are still have having trouble, continue to reply here. If you are not the original author of the post or the post has aged, start a new thread linking this one as relevant to your problem, providing as much additional information as possible.