简体   繁体   中英

NestJS - JWTModule context dependency

After run my app i get this...

[Nest] 5608   - 01.01.2021, 18:12:05   [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 */ ]
  })

can someone tell me what i have wrong with my code?

@Module({
    imports: [TypeOrmModule.forFeature([User]),
    JwtModule.register({
        secretOrPrivateKey: 'secret12356789'
    })
    ],
    providers: [UserService]
})
export class AuthModule { }

@Module({
  imports: [
    TypeOrmModule.forRoot({
      //
    }),
    AuthModule,
    UserModule,
    JwtModule
  ],
  controllers: [AppController, UserController, AuthController ],
  providers: [AppService, UserService, AuthService ],
})
export class AppModule {}

thanks for any help ///////////////////////////////////////////////////////////////

In your AppModule you have the JwtModule imported but adding no options to it. This is what's causing the issue. As you already have the JwtModule registered in the AuthModule , this probably isn't what you're meaning to do.

You have the UserService registered in at least two places ( AuthModule and AppModule ), you're probably meaning to add the UserService to the exports of UserModule and then add the UserModule to the imports array of the module where you need the UserService .

TL;DR See, copy/paste examples.

Recently had the same issue. As Nest.JS recommends the JwtModule could be declared in an authorization module: https://docs.nestjs.com/security/authentication . And, yes, it should be declared once with all its settings. The topic message could come from a multiple declaration (some of them has no JWT_MODULE_OPTIONS ). Can wrap Jay's response with a prepared working example.

So the auth module should look like:

//./authorization/authorization.module.ts

import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';

import { AuthorizationController } from './authorization.controller';
import { AuthorizationService } from './authorization.service';

@Module({
    imports: [
    ...
    JwtModule.register({
        secret: process.env.ACCESS_TOKEN_SECRET || 'SOME_SECURE_SECRET_jU^7',
        signOptions: {
            expiresIn: process.env.TOKEN_EXPIRATION_TIME || '24h',
        }
    }),
    ....
    ],
    controllers: [AuthorizationController],
    providers: [AuthorizationService],
    exports: [AuthorizationService, JwtModule]//<--here exports JwtModule
                                              //as a part of AuthorizationModule 
})

export class AuthorizationModule {};

Then your AppModule will inject the exported JwtModule from AuthorizationModule in this way.

//./app.module.js

import { Module } from '@nestjs/common';
import { AuthorizationModule } from './authorization/authorization.module';

@Module({
    imports: [
        ...
        AuthorizationModule, //<-- here injects the set up JwtModule
                             //NB! No additional injections required!
        ...
    ],
    ...
})

export class AppModule {};

Hope, this will help. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM