#Nest can't resolve dependencies

31 messages · Page 1 of 1 (latest)

uneven summit
#

Hey,

I am currently working on a rest + ws project in Nest.
Worked fine until now and I love the framework.

However, since getting back to the project this mornign I have been receiving the following error:

[Nest] 37008  - 15.02.2024, 00:13:00   ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserResolver (?). Please make sure that the argument dependency at index [0] is available in the UserModule context.

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

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

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

    at Injector.resolveSingleParam (E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\injector.js:191:19)
    at resolveParam (E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\injector.js:128:49)
    at Array.map (<anonymous>)
    at Injector.resolveConstructorParams (E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\injector.js:143:58)
    at Injector.loadInstance (E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\injector.js:70:24)
    at Injector.loadProvider (E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\injector.js:97:20)
    at E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\instance-loader.js:56:33
    at Array.map (<anonymous>)
    at InstanceLoader.createInstancesOfProviders (E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\instance-loader.js:55:36)
    at E:\Daniel\Programmieren\Projekte\Eigene Projekte\Academi.fy\backend\node_modules\@nestjs\core\injector\instance-loader.js:40:24

I know that "Please make sure that the argument **dependency **at [...]" is suggesting a circular dependency.

The issue however is: I can't find it.

user.resolver.ts

@Resolver(() => User)
export class UserResolver {
  constructor(private readonly userService: UserService) {}

  @Query(() => [User])
  async getAllUsers(): Promise<User[]> {
    return this.userService.getAllUsers();
  }
  // ... other functions like getAllUsers
}

user.service.ts

@Injectable()
export class UserService extends Service {
  constructor(private prisma: PrismaService) {
    super();
  }

  async getAllUsers(): Promise<User[]> {
    return this.prisma.user.findMany({
      include: {
        ...userNesting,
      },
    });
  }
  // ... other functions like getAllUsers
}

and user.module.ts

@Module({
  providers: [UserService, UserResolver],
  controllers: [UserController],
})
export class UserModule {}

UserResolver depends on UserService, UserService on PrismaService.
But except for user.module.ts, I have not mentioned UserResolver a single time somewhere else.
UserService is only mentioned in UserController - so also nowhere else.

Any ideas? 🙏
(if any information is missing, please tell me!)

scenic phoenix
#

you could use madge to spot the circularity

#

constructor(@Inject(forwardRef(() => UserService)) private readonly userService: UserService) {}
should fix that

uneven summit
#

Fixed it, but why?

scenic phoenix
#

you got a cirurlarity somehow

#

we can't tell why

uneven summit
uneven summit
scenic phoenix
#

what if you use the entry file instead of just that one?

#

src/main.ts I guess

uneven summit
scenic phoenix
#

pretty weird

uneven summit
#

Yeah...

#

The thing is it worked before. I don't know what I changed that resulted in this. I haven't worked on the user modules for at least a week

scenic phoenix
#

can you share the full code?

uneven summit
#

sure

scenic phoenix
#

can you share the steps to reproduce that error?

#

I manage to reproduce it by running npm start

#

idk where's the issue but the barrel file src/rest/index.ts might help on creating a circularity

uneven summit
#

Yeah, I read online that somebody solved the issue by rearranging an index.ts. Still I can't seem to find how this can be applied to my issue

scenic phoenix
#

I didn't spot the circularity but there is one for sure:

#

if you add console.log({ UserService }) in the beginning of user.resolver.ts, you'll see that it's undefined

#

so that's not a nestjs bug, at least

livid monolithBOT
#

This post has been marked as resolved. :white_check_mark:
Please read through the conversation and resolution if you are having the same issue, and then re-open the post if you are still having trouble, providing as much extra information as possible.

scenic phoenix
#

not sure why Madge didn't caught it neither

uneven summit
#

Hmmm, alright. So why exactly is it undefined? How can I find the circularity ?

#

Because in user.module.ts nothing is undefined

#

And these are the only usages

#

Got it working by removing all controllers, modules, resolvers and services from barrell files ✅