Closed CSenshi closed 1 year ago
that's due to circular imports with barrel files
Just don't import from '.'
. There's no good reason to do that. It's limitation that nestjs cannot solve without using forwardref
More on this here: Circular Dependencies in NestJS and how to Avoid Them.
@Inject('SOME_TOKEN')
. So why is nest unable to solve it? Maybe use custom injection token when DIC finds circular import?So why is nest unable to solve it?
because nestjs doesn't have enough metadata to do so like you did.
You can understand it better if you understand nodejs CJS module system and typescript. That's not really related to nestjs itself.
Also, I'd suggest you to read this: https://github.com/nestjs/docs.nestjs.com/pull/2481/files to understand how @Injectable()
works. Might be helpful
forwardRef is the solution
I don't think so. Most of the time it's an workaround for a design problem on your side
Okay I found out about something. You can actually do this. You just need to include file that uses circular DI last in the barrel file.
Before:
export * from './controller';
export * from './service';
After:
export * from './service';
export * from './controller';
As far as controller uses service and not vice versa this works
Is there an existing issue for this?
Current behavior
When trying to import from current working directory nest is not able to inject provider.
Error: Nest can't resolve dependencies of the HttpController (?). Please make sure that the argument dependency at index [0] is available in the AppModule context.
Minimum reproduction code
https://github.com/CSenshi/nest-dic-bug-demo
Steps to reproduce
Expected behavior
It should just resolve dependancy.
Package
Other package
No response
NestJS version
^10.0.0
Packages versions
Node.js version
20
In which operating systems have you tested?
Other
No response