2

I am using @nestjs/jwt in my NestJS project.

I have two modules, AuthModule and AppModule.

  • The AuthModule uses the @nestjs/jwt
  • The AppModule invoke auth service from AuthModule.

AuthService:

import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ForbiddenException } from '@nestjs/common';

@Injectable()
export class AuthService {
  constructor(private readonly jwt: JwtService) {}

  async validate(token: string) {
    return this.jwt.verify(token);
  }
   ...
}

AuthModule:

import { Module } from '@nestjs/common'
import { TokenService } from './auth.service'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { JwtModule } from '@nestjs/jwt'

@Module({
  imports: [
    JwtModule.register({
          secret: "mysecret",
          signOptions: { expiresIn: "60s" },
        }),
  ],
  // I provide AuthService & JwtService
  providers: [AuthService, JwtService], 
  // I export AuthService and JwtService
  exports: [AuthService, JwtService],
})
export class AuthModule {}

AppModule:

@Module({
  imports: [
    AuthModule,
    ...
  ],
  controllers: [AppController],
  // I provide AuthService & JwtService also in AppModule
  providers: [AppService, JwtService],
})
export class AppModule {}

(The AppController invokes AuthService instance to validate token.)

I constantly get error:

Nest can't resolve dependencies of the JwtService (?). Please make sure that the argument JWT_MODULE_OPTIONS at index [0]

Why is that? Where do I miss?

user842225
  • 5,445
  • 15
  • 69
  • 119

1 Answers1

3

You don't need to add JwtService to the providers. Nest will look through the current module's providers, then the imported module's exports, and then the global module's exports to find providers that are not immediately in the level it's resolving the providers in. This means that for AuthService it'll look up to the JwtModule's exports and find the JwtService already created, and use that instance. Then in the AppModule you don't need to mention the AuthService or JwtService, just import the AuthModule that exports the AuthService and JwtModule (doing some module re-exporting) and you should be good to go


EDIT: Added module code:

AuthModule

import { Module } from '@nestjs/common'
import { TokenService } from './auth.service'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { JwtModule } from '@nestjs/jwt'

@Module({
  imports: [
    JwtModule.register({
          secret: "mysecret",
          signOptions: { expiresIn: "60s" },
        }),
  ],
  providers: [AuthService],
  exports: [AuthService, JwtModule],
})
export class AuthModule {}

AppModule

@Module({
  imports: [
    AuthModule,
    ...
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • Thanks, your answer was actually my 1st version code, however since I keep getting that error, I then explictly provide `AuthService` and `JwtService` in both `AuthModule` and `AppModule`. I tried again removing the two services in provider array of `AppModule` only import the `AuthModule`, the error is the same. – user842225 Nov 30 '21 at 16:19
  • What was the error with the first version, because it probably was not the same, but similar. The current error is because you have `JwtService` in a `providers` array. – Jay McDoniel Nov 30 '21 at 16:23
  • Exactly the same error I meant. Like I said I tried again reverting back to 1st version, it is indeed exactly the same error. – user842225 Nov 30 '21 at 16:58
  • Do you still have `JwtService` in a `providers` array then? – Jay McDoniel Nov 30 '21 at 16:58
  • I have the `JwtService` in the `providers` of `AuthModule` not `AppModule` – user842225 Nov 30 '21 at 18:53
  • First line of my answer. **You don't need to add `JwtService` to the `providers`.** That goes for **all** `providers` arrays, because the `JwtModule` already creates the `JwtService` and `exports` it. – Jay McDoniel Nov 30 '21 at 18:55
  • I actually tried that removing `JwtService` from providers of both module. same error. – user842225 Nov 30 '21 at 19:09
  • I think it is easier if you just provide the code of the modules which you feel should work. I have tried all things I can come up with including what we discussed, but it is always same error. – user842225 Nov 30 '21 at 19:11
  • I've edited your modules and added them as code snippets. With what information you've provided in the post, this should all be correct – Jay McDoniel Nov 30 '21 at 19:28
  • ```Nest can't resolve dependencies of the JwtService (?)```, same. But thanks. – user842225 Nov 30 '21 at 19:36
  • Then you have `JwtService` somewhere else in a `providers` or you haven't recompiled your code. Based on what you've provided in your question, there's no more help I can provide. If you want to drop a link to your git repo, I can take a look – Jay McDoniel Nov 30 '21 at 19:36
  • No, I don't have `JwtService` elsewhere. But thank you, I am not able to provide the repo since it is company codebase. I will figure out why. – user842225 Nov 30 '21 at 21:34