Search code examples
node.jsnestjs

ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserService


I have a nest js app with Auth and User modules The Auth module is written like this It imports the User module due to the AuthService which uses it

import { Module, forwardRef } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { GoogleStrategy } from './google.strategy';
import { PassportModule } from '@nestjs/passport';
import { JwtAuthModule } from 'src/jwt/jwt.module';
import { UserModule } from 'src/user/user.module';

@Module({
  controllers: [AuthController],
  providers: [AuthService, GoogleStrategy],
  imports: [
    UserModule,
    JwtAuthModule,
    PassportModule.register({ defaultStrategy: 'google' }),
  ],
  exports: [AuthService],
})
export class AuthModule {}

The User module is written like this

import { Module, forwardRef } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { PrismaModule } from 'src/prisma/prisma.module';
import { JwtAuthModule } from 'src/jwt/jwt.module';
import { AuthModule } from 'src/auth/auth.module';
import { UploadModule } from 'src/upload/upload.module';

@Module({
  controllers: [UserController],
  providers: [UserService],
  imports: [
    PrismaModule,
    JwtAuthModule,
    forwardRef(() => AuthModule),
    UploadModule,
  ],
  exports: [UserService],
})
export class UserModule {}

It uses forwardRef to avoid circular dependency error

and the UserService is using the AuthService like this

@Injectable()
export class UserService {
  constructor(
    private readonly authService: AuthService,
    private readonly prismaService: PrismaService,
  ) {}

Here is the App module also

import { Module, ValidationPipe } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
import { APP_PIPE } from '@nestjs/core';
import { UploadModule } from './upload/upload.module';
import { ImageModule } from './image/image.module';
import { UserModule } from './user/user.module';
import { PrismaModule } from './prisma/prisma.module';

@Module({
  imports: [
    ConfigModule.forRoot(),
    UserModule,
    AuthModule,
    UploadModule,
    ImageModule,
    PrismaModule,
  ],
  providers: [
    {
      provide: APP_PIPE,
      useClass: ValidationPipe,
    },
  ],
})
export class AppModule {}

But it throws this error

ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserService (?, PrismaService). Please make sure that the argument dependency at index [0] is available in the UserModule context.

Potential solutions:
- Is UserModule a valid NestJS module?
- If dependency is a provider, is it part of the current UserModule?
- If dependency is exported from a separate @Module, is that module imported within UserModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

I tried to add services to app module and a lot of things but nothing helped


Solution

  • I found the Error on the docs Here is the new UserService

    @Injectable()
    export class UserService {
      constructor(
        @Inject(forwardRef(() => AuthService))
        private readonly authService: AuthService,
        private readonly prismaService: PrismaService,
      ) {}