最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nest can't resolve dependencies of the JwtService - Stack Overflow

programmeradmin5浏览0评论

I have created some modules here but i am facing an error

Auth module

   import { Module } from "@nestjs/mon";
import { AuthService } from "./auth.service";
import { LocalStrategy } from "./local.strategy";
import { JwtStrategy } from "./jwt.strategy";
import { UsersModule } from "../users/users.module";
import { PassportModule } from "@nestjs/passport";
import { JwtModule, JwtService } from "@nestjs/jwt";
import { jwtConstants } from "./constants";
import { ConfigModule, ConfigService } from "@nestjs/config";

@Module({
    imports: [
        UsersModule,
        PassportModule,
        JwtModule.register({
            secret: jwtConstants.secret,
            signOptions: { expiresIn: "1d" },
        }),
    ],
    providers: [AuthService, LocalStrategy, JwtStrategy],
    exports: [AuthService, LocalStrategy, JwtStrategy],
})
export class AuthModule {}

Email module

  import { Module } from "@nestjs/mon";
import EmailService from "./email.service";

import { ConfigModule } from "@nestjs/config";

import { EmailConfirmationService } from "./emailConfirmation.service";
import { EmailConfirmationController } from "./emailConfirmation.controller";
import { EmailConfirmationGuard } from "./guards/emailConfirmation.guard";
import { AuthModule } from "src/auth/auth.module";
import { UsersModule } from "src/users/users.module";

@Module({
    imports: [ConfigModule,AuthModule,UsersModule],
    providers: [EmailService,EmailConfirmationService,EmailConfirmationGuard],
    exports: [EmailConfirmationService,EmailConfirmationGuard],
    controllers : [EmailConfirmationController]
})
export class EmailModule {}

User module

import { Module } from "@nestjs/mon";
import { UsersService } from "./users.service";
import { UsersController } from "./users.controller";
import { MongooseModule } from "@nestjs/mongoose";
import { UserSchema } from "./entities/user.entity";
import { EmailModule } from "src/email/email.module";

@Module({
    imports: [MongooseModule.forFeature([{ name: "User", schema: UserSchema }]),EmailModule],
    providers: [UsersService],
    exports: [UsersService],
    controllers: [UsersController],
})
export class UsersModule {}

Error I am facing

 [Nest] 9200  - 09/26/2021, 3:43:15 PM   ERROR [ExceptionHandler] Nest cannot create the EmailModule instance.
The module at index [1] of the EmailModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: 
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> AuthModule -> UsersModule]
Error: Nest cannot create the EmailModule instance.
The module at index [1] of the EmailModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: 
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

What am i missing ?

EmailConfirmationService is used in UsersController
UserService is used in EmailConfirmationService

I have created some modules here but i am facing an error

Auth module

   import { Module } from "@nestjs/mon";
import { AuthService } from "./auth.service";
import { LocalStrategy } from "./local.strategy";
import { JwtStrategy } from "./jwt.strategy";
import { UsersModule } from "../users/users.module";
import { PassportModule } from "@nestjs/passport";
import { JwtModule, JwtService } from "@nestjs/jwt";
import { jwtConstants } from "./constants";
import { ConfigModule, ConfigService } from "@nestjs/config";

@Module({
    imports: [
        UsersModule,
        PassportModule,
        JwtModule.register({
            secret: jwtConstants.secret,
            signOptions: { expiresIn: "1d" },
        }),
    ],
    providers: [AuthService, LocalStrategy, JwtStrategy],
    exports: [AuthService, LocalStrategy, JwtStrategy],
})
export class AuthModule {}

Email module

  import { Module } from "@nestjs/mon";
import EmailService from "./email.service";

import { ConfigModule } from "@nestjs/config";

import { EmailConfirmationService } from "./emailConfirmation.service";
import { EmailConfirmationController } from "./emailConfirmation.controller";
import { EmailConfirmationGuard } from "./guards/emailConfirmation.guard";
import { AuthModule } from "src/auth/auth.module";
import { UsersModule } from "src/users/users.module";

@Module({
    imports: [ConfigModule,AuthModule,UsersModule],
    providers: [EmailService,EmailConfirmationService,EmailConfirmationGuard],
    exports: [EmailConfirmationService,EmailConfirmationGuard],
    controllers : [EmailConfirmationController]
})
export class EmailModule {}

User module

import { Module } from "@nestjs/mon";
import { UsersService } from "./users.service";
import { UsersController } from "./users.controller";
import { MongooseModule } from "@nestjs/mongoose";
import { UserSchema } from "./entities/user.entity";
import { EmailModule } from "src/email/email.module";

@Module({
    imports: [MongooseModule.forFeature([{ name: "User", schema: UserSchema }]),EmailModule],
    providers: [UsersService],
    exports: [UsersService],
    controllers: [UsersController],
})
export class UsersModule {}

Error I am facing

 [Nest] 9200  - 09/26/2021, 3:43:15 PM   ERROR [ExceptionHandler] Nest cannot create the EmailModule instance.
The module at index [1] of the EmailModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs./fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> AuthModule -> UsersModule]
Error: Nest cannot create the EmailModule instance.
The module at index [1] of the EmailModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs./fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

What am i missing ?

EmailConfirmationService is used in UsersController
UserService is used in EmailConfirmationService
Share Improve this question edited Sep 26, 2021 at 13:45 Dren asked Sep 24, 2021 at 13:27 DrenDren 1,0991 gold badge10 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You have JwtService listed in your imports. Imports are for modules only.

Share the code from JwtService as well so that we can make sure there are no other issues.

Update: If the EmailModule wants to use the exports from the AuthModule (JwtService in this case), it must add the AuthModule to its imports array.

This is the entire premise of the DI system, modules share things between eachother by placing the thing they intend to share in the exports array. After that, any module can add the Source module to its imports array to gain access to the things that the source exported. The error message literally spells it out for you:

If JwtService is exported from a separate @Module, is that module imported within EmailModule? @Module({ imports: [ /* the Module containing JwtService */ ] })

In order to use EmailService in you UserController and also UserService is used in EmailConfirmationService then you have to do something like this in UserModule:

imports: [forwardRef(() => EmailModule)]

and inside the EmailModule do the same thing for the UserModule:

imports: [forwardRef(() => UserModule)]

Rest of the imports should be without the forwardRef(()=>)

you can read more about circular dependency here https://docs.nestjs./fundamentals/circular-dependency

Make sure your target in tsconfig.json is es6.

发布评论

评论列表(0)

  1. 暂无评论