#Use moduleRef.resolve for exports in imported module

6 messages · Page 1 of 1 (latest)

left torrent
#

I'm trying to use moduleRef.resolve to create a provider that is an exported in another module that is imported in the current module.

I'm getting the error
Error: Nest could not find [Provider] element (this provider does not exist in the current context)

I followed the discussion here https://github.com/nestjs/nest/issues/5487 and the suggested fix does not work.

Any ideas on how to get this to work? The last person on that thread shares my confusion. Why am I able to import the provider with normal dependency injection but not with moduleRef.resolve?

GitHub

Bug Report Current behavior This is an extension on the issue of the bug that was fixed in closed issue #4756 When creating an injectable class using ModuleRef.resolve() the module is not found if ...

unborn lantern
#

Any reason you're using resolve? Is it transient or request scoped?

left torrent
#

No it is just a normal injected service. I thought resolve was the recommended method to use.

#

I tried create but it seemed to have a similar issue

unborn lantern
#

If it's a singleton use get instead. Try moduleRef.get(InjectionToken, { strict: false })

left torrent
#

Wow what do you know that did it. Thanks! ❤️
So nest instantiates all the services in the sub modules automatically even if they aren't injected? And then I can use .get() to access them.