nest: Application crash when using barrel files (multi index.ts)
I’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Having
/* InvitationsModule
*********************** */
import { Module } from '@nestjs/common';
import { InvitationComposerService, InvitationsService } from './services';
import { DatabaseModule } from '../database';
import { AuthModule } from '../auth';
import { MailModule } from '../mail';
import { invitationModelProvider } from './providers';
@Module({
imports: [
DatabaseModule.forRoot([invitationModelProvider]),
AuthModule,
MailModule,
],
providers: [InvitationsService, InvitationComposerService],
exports: [InvitationsService],
})
export class InvitationsModule {}
/* AuthModule
*********************** */
import { Module } from '@nestjs/common';
import { ExternalAuthModule } from '../external-auth';
import { AuthService, ExternalAuthAdapterService } from './services';
import { AuthController } from './auth.controller';
import { AuthMiddleware } from './auth.middleware';
@Module({
imports: [ExternalAuthModule],
providers: [AuthService, ExternalAuthAdapterService, AuthMiddleware],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {}
where ExternalAuthModule and MailModule have no module imports,
I have this:

which leads to
Error: Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it. (Read more https://docs.nestjs.com/advanced/circular-dependency.) Scope [TestModule -> InvitationsModule -> AuthModule]
Expected behavior
It should work
Minimal reproduction of the problem with instructions
Not sure. We’ve got nearly 50 modules and all are working fine.
And most of them actually use AuthModule as you might guess.
Only this one, that I’ve created recently can’t load. Any ideas on what am I doing wrong? 😃
What is the motivation / use case for changing the behavior?
Environment
Nest version: 5.0.1
Actually, i've got the same is on v4
For Tooling issues:
- Node version: 10.10.0
- Platform: Mac
Others:
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (4 by maintainers)
I have changed the issue title to make this thread more descriptive and easier to find by other people. Anyway, I don’t think that we have to track this, this error is not related to Nest itself. I would suggest omitting barrel files when it comes to module’s providers/module class OR using them very carefully (for example, even if you create a barrel, you shouldn’t use it from within the same module itself [only from outside] and when circular dependency may potentially appear).
As far as I know, barrels are no longer recommended due to this issue. It is nothing related to nest itself
Seems like a dublicate of #825. I can confirm @alexandr2110pro behavior. I am absolutely clueless why this happens. It only occuree to me inside a Nest application context, which is super strange. I think we should track this issue somewhere and do not close it again. Maybe it is a bug of TypeScript or maybe of Nest. Nonetheless we should track it
@marcus-sa
No, there’re no circular dependencies.
I know about
forwardRef. It is not the case here.Please check the prev. comment. The undefined is the ts import, not the NestJS import.