Im trying to import a Module with registerAsync and configure it with a provider I have in my module, but it throws an error it cant find this provider. What am I missing?
My Code:
import { CacheModule, Module } from '@nestjs/common';
@Module({
imports: [
CacheModule.registerAsync({
useFactory: async (options) => ({
ttl: options.ttl,
}),
inject: ['MY_OPTIONS'],
}),
],
providers: [
{
provide: 'MY_OPTIONS',
useValue: {
ttl: 60,
},
},
],
})
export class AppModule {
}
Error:
Error: Nest can't resolve dependencies of the CACHE_MODULE_OPTIONS (?). Please make sure that the argument MY_OPTIONS at index [0] is available in the CacheModule context.
The example above is a simplification of my code. But the main issue stays the same: I do have a provider within the AppModule and I need it in the CacheModule.registerAsync() function.
If anyone wants to try figuring this out I made a really simple repository: https://github.com/MickL/nestjs-inject-existing-provider
I'd say the problem is "CacheModule.registerAsync" and "AppModule" work at different levels, so defining a provider on "AppModule" does not make it available for "CacheModule.registerAsync".
Assuming we move the "MY_OPTIONS" to another module: "CacheConfigModule", it could look like:
CacheConfigModule.ts
import { Module } from '@nestjs/common';
@Module({
providers: [
{
provide: 'MY_OPTIONS',
useValue: {
ttl: 60
}
}
],
exports: ['MY_OPTIONS']
})
export class CacheConfigModule {}
AppModule.ts
import { CacheModule, Module } from '@nestjs/common';
import { CacheConfigModule } from './CacheConfigModule';
@Module({
imports: [
CacheConfigModule,
CacheModule.registerAsync({
imports: [CacheConfigModule],
useFactory: async (options) => ({
ttl: options.ttl
}),
inject: ['MY_OPTIONS']
})
]
})
export class AppModule {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With