#Import service to another module error

14 messages · Page 1 of 1 (latest)

worthy pawn
#

Hey I'm pretty new to the NestJS, can anybody explain me why is this happening?

Error:
Nest can't resolve dependencies of the AuthService (?, JwtService, PrismaService). Please make sure that the argument dependency at index [0] is available in the AuthModule context.

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [config],
    }),
    AuthModule,
    PrismaModule,
    UserModule,
   ],
})
export class AppModule {}

user.module.ts

@Module({
  providers: [UserService, PrismaService, AuthService, JwtService],
  controllers: [UserController],
  imports: [TypedEventEmitterModule],
})
export class UserModule {}

user.service.ts

export class AuthService {
  constructor(
    private userService: UserService,
    private jwtService: JwtService,
    private prisma: PrismaService,
  ) {}
}

auth.module.ts

@Module({
  controllers: [AuthController],
  providers: [AuthService, UserService, PrismaService, JwtService],
})
export class AuthModule {}
low streamBOT
#

Suggestion for @worthy pawn:
Don't list a service in providers array of a module unless the module really provides the service.
If your ServiceA depends on a ServiceB from different module ModuleB, add ServiceB to ModuleBs exports array,
then add ModuleB to imports array in your module.

@Module({
  providers: [ServiceB],
  exports: [
    ServiceB // <-- export the "ServiceB" provider by adding it to the module's exports array
  ], 
})
export class ModuleB {}
@Module({
  imports: [
    ModuleB // <-- modules that wish to inject the "ServiceB" will need to import the "ModuleB" in their imports array
  ],  
  providers: [ServiceA],
})
export class ModuleA {}
import { ServiceB } from '../module-b/service-b.service'

@Injectable()
export class ServiceA {
  constructor(private readonly serviceB: ServiceB) {}
  
  // async foo() {
  //   this.serviceB.bar();
  // }
}
worthy pawn
#

I'm really confused, so it should look like this?

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [config],
    }),
    AuthModule,
    PrismaModule,
    UserModule,
   ],
})
export class AppModule {}

user.module.ts

@Module({
  providers: [UserService, PrismaService, JwtService], // AuthService deleted
  controllers: [UserController],
  imports: [TypedEventEmitterModule, AuthModule], // AuthModule added

})
export class UserModule {}

user.service.ts

export class AuthService {
  constructor(
    private userService: UserService,
    private jwtService: JwtService,
    private prisma: PrismaService,
  ) {}
}

auth.module.ts

@Module({
  controllers: [AuthController],
  providers: [AuthService, UserService, PrismaService, JwtService], 
  exports: [AuthService], // export AuthService added
})
export class AuthModule {}
inland mortar
#

I think so
what's the error now?

worthy pawn
#

This is the error now: ERROR [ExceptionHandler] Nest can't resolve dependencies of the AuthService (?, JwtService, PrismaService). Please make sure that the argument dependency at index [0] is available in the AuthModule context.

inland mortar
worthy pawn
#

Yes I was looking that, AuthService provider is already in AuthModule

inland mortar
#

that's not the case

#

read the docs again

worthy pawn
#

so there is no actual way to use ServiceA in ModuleB and ServiceB in ModuleA because it is circular error...

inland mortar
#

there is a way
as shown in the docs and in the article above

#

but I'd suggest you to try to fix the circular import

worthy pawn
#

I manage to fix my problem, thank you for support 🤲