For example: i have a root module and 2 children AuthModule and UsersModule.
RootModule:
@Module({
imports: [
AuthModule,
PrismaModule,
ConfigModule.forRoot({ isGlobal: true }),
UsersModule,
],
controllers: [],
providers: [
{
provide: APP_PIPE,
useClass: ValidationPipe,
},
],
})
AuthModule:
@Global()
@Module({
imports: [
JwtModule.register({
global: true,
secret: process.env.JWT_SECRET_KEY || "secret key",
signOptions: { expiresIn: "2d" },
}),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
exports: [AuthService],
})
UsersModule:
@Global()
@Module({
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
I am using the AuthService provider from AuthModule to UsersModule:
@Injectable()
export class UsersService {
constructor(
private readonly prisma: PrismaService,
private readonly authService: AuthService,
) {}
}
And i get en error:
Error: Nest can’t resolve dependencies of the UsersService (PrismaService, ?). Please make sure that the argument dependency at index [1] is available in the UsersModule context.
I created mockservice and export it from AuthModule and it works! I don’t understand what the problem with this service…
If the problem is cyclical dependency, then how do I solve it from an architectural point of view?
Dependencies in AuthService:
@Injectable()
export class AuthService {
constructor(
private readonly usersService: UsersService,
private readonly jwtService: JwtService,
) {}
}
Dmitriy Dultsev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.