#Is it important that transient scoped modules are injected using @Inject?
16 messages · Page 1 of 1 (latest)
Is it important that transient scoped modules are injected using @fading burrow
No, the@Injectdecorator 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
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
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 asfile:, because it is using a different copy of Nest's source
ohhh. so in that case what would be the solution? I'm using a nx monorepo with one big, shared node_modules at the root.
The @fading burrow did seem to solve my issue but evidently not for the reason I thought
nx monorepo is fine, it links libraries using typescript path mapping instead of symlinks
what is the exact problem you're facing?
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
Ah, I see, you're missing the @Injectable decorator on top of ExampleService
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 😅
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
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.
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