#Is it important that transient scoped modules are injected using @Inject?

16 messages · Page 1 of 1 (latest)

cerulean barn
#

And is this the case for all non-default scopes? I think I have found the reason why my transient scoped module that I have in a library sometimes is unable to be resolved by Nest :p

desert summit
#

Is it important that transient scoped modules are injected using @fading burrow
No, the @Inject decorator is only there for cases when the injection token is not the same as the class type of the argument.
The scope of the provider itself is determined inside the @Injectable({ scope: xx }) decorator.

BUT in case a transient (or a singleton) scoped provider inject s request-scoped one, the request scope wins. https://docs.nestjs.com/fundamentals/injection-scopes#scope-hierarchy

#

module that I have in a library sometimes is unable to be resolved by Nest
This is a known problem if the libary is linked locally as file:, because it is using a different copy of Nest's source

cerulean barn
desert summit
#

what is the exact problem you're facing?

cerulean barn
#

so the service that I had transient scoped was a logger library, which I use in all my nestjs microservices. I've got it set up so I can set the context in each module like this:

export class ExampleService {
  constructor(
    private readonly logger: LoggerService
  ) {
    this.logger.setContext(ExampleService.name);
  }

Sometimes however (not always), I get

TypeError: Cannot read properties of undefined (reading 'setContext')
    at new ExampleService
...
#

when i say sometimes, it only happens in SOME modules

Like I have 4 modules for handling oauth using google, twitter etc. they all depend on a common module, CommonOAuthService. The setContext works perfect in the 4 modules but the common one gets this setContext error without @ Inject

desert summit
#

Ah, I see, you're missing the @Injectable decorator on top of ExampleService

cerulean barn
#

omg 😭

#

you're right

desert summit
#

what that decorator does (or any decorator over a class) is it triggers typescript to emit type metadta to the javascript compiled source (that is the emitDecoratorMetadata feature in your tsconfig). Nest can then read those and inject the necessary things. But for that to happen, the metadata must be there.

#

the @Inject decorator does the same explicitly, but for most cases, the automatic detection is enough, that is, if you don't forget the @Injectable decorator 😅

cerulean barn
#

damn that explains things. I actually had the same issue using turborepo, now I'm beginning to wonder if I made the same mistake there or if it was because of the symlink issue with nestjs/core. Half the reason why I switched over to nx 😭

but it has been much nicer to work with regardless

desert summit
#

Btw, I just want to clear something up - the Injectable decorator says "I want NestJS to inject stuff into this class". What many people think it means is "I want this class to be injected in other stuff", which is not the case. If you have classes that don't inject anything, you can leave out the decorator, but Nest will happily inject them to other things.

cerulean barn
#

Thank you for clearing that up man, I feel so dumb now but at the same time I learned something, so I'm happy about that! You are a great person