3
votes

Can someone tell me why i have this error:

[Nest] 556   - 2020-06-10 18:52:55   [ExceptionHandler] Nest can't resolve dependencies of the JwtService (?). Please make sure that the argument JWT_MODULE_OPTIONS at index [0] is available in the JwtModule context.

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

my modules:

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt'}),
    JwtModule.register({
      secret: 'topSecret51',
      signOptions: {
        expiresIn: 3600
      },
    }),
    TypeOrmModule.forFeature([User])
  ],
  controllers: [AuthController],
  providers: [AuthService, UserService]
})
export class AuthModule {}
@Module({
  controllers: [UserController],
  providers: [UserService, AuthService],
  imports: [AuthModule]
})
export class UserModule {}
@Module({
  imports: [
    TypeOrmModule.forRoot(typeOrmConfig),
    UserModule,
    AuthModule
  ],
})
export class AppModule {}

i try to change in all of them but in all of them my app does not work

thanks for any help

////////////////////////////////////////////////////

2
Can you show the constructors of the UserService and AuthService? Where all is the JwtService used? Are you common and core version the same, and the @nestjs/jwt package up to date? - Jay McDoniel

2 Answers

0
votes

If you are using some dependencies of the controller in JwtService, you need to add JWT_MODULE_OPTIONS as provider inside the current AuthModule. Have a look at the nestjs documentation. They have explained on how to register custom providers.

0
votes

I received this error when I did not correctly import / initialize the JwtModule. The JwtModule should handle the JwtService initialization which this error comes from. I would start there. If you're using the AuthModule in your test environment which by extension needs the JwtModule then you'll need to manually define your JwtModule with something similar:

    beforeAll(async () => {
        const testingModule: TestingModule = await Test.createTestingModule({
            imports: [
                ConfigModule.forRoot({ isGlobal: true }),
                MongooseModule.forFeature([{ name: 'User', schema: userSchema }]),
                JwtModule.registerAsync({
                    imports: [ConfigModule],
                    inject: [ConfigService],
                    useFactory: async (configService: ConfigService) => ({
                        secret: configService.get('JWT_SECRET'),
                        signOptions: { expiresIn: '1d' }
                    })
                }),
            ],