#Nest can't resolve dependencies; Build error

5 messages · Page 1 of 1 (latest)

grizzled wren
#

Hey everyone! I am currently running into this nasty problem and after hours of searching, I have only found a semi satisfying solution so far and I am curious if anybody has experienced something similar:

I get the usual error message:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the InventoryElementsService (PrismaService, ?, AuthorizerService). Please make sure that the argument dependency at index [1] is available in the InventoryElementsModule context.

The respective constructor of InventoryElements.service.ts looks like this:

@Injectable()
export class InventoryElementsService {
    constructor(
        private readonly prisma: PrismaService,
        private readonly notificationsService: NotificationsService,
        private readonly authorizer: AuthorizerService,
    ) { }
...

This means, that NotificationsService could not be properly injected.
Looking at the corresponding InventoryElements.module.ts it seems like that I do import it properly: (see image 1)
It is a circular dependency and therefore I use forwardRef(() => ...).

The imported notifications.module.ts also seems to be initialised correctly and the **notifications.service.ts **is also marked with Injectable() (see image 2)

Now, interestingly, if I inject it inline into the InventoryElements.service.ts-constructor directly, it works: (see image 3)

This solution does not seem ideal and I am pretty sure that there is something wrong with my code, but I am really running out of options, where to look for mistakes (and yes I have already double checked my imports 🙈)

Thanks for any help and stay healthy!

#

Sorry this was meant to be image 3

silent anchor
grizzled wren
#

Oh ok, so it is not enough to try to resolve the circular dependency on the module level? It needs to be done on the provider level with @Inject(forwardRef())?

silent anchor
#

You need it at both levels. This is because both the modules and the providers are circular