I want to create a NestJS module I will host on my Verdaccio repository and reuse it into multiple projects, specially into guards. This module use the configService of the app where it is imported as a dependency. But when I import it into the app, I have dependency problems. I don't understand what's happening. Can someone help me to understand and solve this problem ?
First, the module files: myModule.module.ts
import { Module } from '@nestjs/common';
import { MyService } from './myModule.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [ConfigModule],
providers: [MyService,ConfigService],
exports: [MyService],
})
export class MyModule {}
myModule.service.ts
import { Inject, Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
@Injectable()
export class OIDCService {
private readonly logger:Logger;
constructor(@Inject("ConfigService") private readonly configService:ConfigService){
this.logger = new Logger("MyService");
}
// ...
myMethod(){
const param = this.configService.get("param");
}
}
Then, the app files:
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule,ConfigService } from '@nestjs/config';
import configValidator from './config/config.validator';
import { MyModule, myService } from 'myModule';
import configSchema from './config/config.schema';
@Module({
imports: [ConfigModule.forRoot({
isGlobal: true,
validationSchema: configValidator,
cache: true,
load:[configSchema]
}),ConfigModule],
controllers: [AppController],
providers: [ConfigService,AppService,MyModule,MyService,ConfigModule],
})
export class AppModule {}
myGuard.ts
import { CanActivate, ExecutionContext, Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { Request } from 'express';
import { Observable } from 'rxjs';
import { MyService } from 'myModule';
@Injectable()
export class MyGuard implements CanActivate {
constructor(@Inject() private readonly myService:MyService){}
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
//...
return (
this.myService.myMethod()
.then(//...)
)
}
}
This is the nest error : Nest can't resolve dependencies of the OIDCModule (?). Please make sure that the argument ConfigModule at index [0] is available in the AppModule context.
Thanks !