#The module at index [5] of the <nameModule> "imports" array is undefined.

47 messages · Page 1 of 1 (latest)

somber pelican
#

I'm confused by the error referred to by nestjs, the full error is like this

Error: Nest cannot create the AuthModule instance.
The module at index [5] of the AuthModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [5] is of type "undefined". Check your import statements and the type of the module

Scope [AppModule -> AuthModule -> UsersModule]

AppModule

// Import

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      ...
    }),
    AuthModule,
    UsersModule,
  ],
})
export class AppModule {}

AuthModule

// Import
@Module({
  imports: [
    ...
    forwardRef(() => UsersModule),
  ],
  controllers: [AuthController],
  providers: [AuthService, JwtStrategy],
  exports: [AuthService],
})
export class AuthModule {}

UsersModule

// Import Module

@Module({
  imports: [
    ...
    forwardRef(() => AuthModule),
  ],
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}

Even though the code already applies forwardRef, can anyone help?

olive wind
#

Can you show us the full modules

somber pelican
olive wind
#

Specifically UsersMoudle and AuthModule

somber pelican
# olive wind Specifically UsersMoudle and AuthModule
import { Module, forwardRef } from '@nestjs/common';
import { UsersService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from './entities/user.entity';
import { CryptoModule } from '@/common/crypto/crypto.module';
import { UsersController } from './users.controller';
import { AuthModule } from '@/auth/auth.module';
import { CompanyModule } from '@/company/company.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([UserEntity]),
    CryptoModule,
    forwardRef(() => AuthModule),
    forwardRef(() => CompanyModule),
  ],
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}

#
import { Module, forwardRef } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from '@/users/entities/user.entity';
import { RefreshTokenEntity } from './entities/refresh-token.entity';
import { CryptoModule } from '@/common/crypto/crypto.module';
import { AuthService } from './auth.service';
import { JwtStrategy } from './strategies/jwt.strategy';
import { AuthController } from './auth.controller';
import { CompanyModule } from '@/company/company.module';
import { WorkspaceModule } from '@/workspace/workspace.module';
import { UsersModule } from '@/users/users.module';

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      useFactory: (configService: ConfigService) => ({
        secret: configService.get('JWT_SECRET'),
        signOptions: {
          expiresIn: configService.get('JWT_EXPIRES_IN'),
        },
      }),
      inject: [ConfigService],
    }),
    TypeOrmModule.forFeature([UserEntity, RefreshTokenEntity]),
    CryptoModule,
    forwardRef(() => UsersModule),
    CompanyModule,
    WorkspaceModule,
  ],
  controllers: [AuthController],
  providers: [AuthService, JwtStrategy],
  exports: [AuthService],
})
export class AuthModule {}
olive wind
#

Can you show CompanyModule?

somber pelican
# olive wind Can you show `CompanyModule`?
import { Module, forwardRef } from '@nestjs/common';
import { CompanyService } from './company.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CompanyEntity } from './entities/company.entity';
import { CryptoModule } from '@/common/crypto/crypto.module';
import { CompanyController } from './company.controller';
import { UsersModule } from '@/users/users.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([CompanyEntity]),
    CryptoModule,
    forwardRef(() => UsersModule),
  ],
  controllers: [CompanyController],
  providers: [CompanyService],
  exports: [CompanyService],
})
export class CompanyModule {}

olive wind
#

And CryptoModule

somber pelican
# olive wind And `CryptoModule`
import { Module } from '@nestjs/common';
import { CryptoService } from './crypto.service';

@Module({
  providers: [CryptoService],
  exports: [CryptoService],
})
export class CryptoModule {}
#

does relational in typeorm matter?

import {
  Column,
  Entity,
  JoinColumn,
  OneToOne,
  PrimaryGeneratedColumn,
  Index,
} from 'typeorm';
import { UserEntity } from '@/users/entities/user.entity';

@Entity('verification_login')
export class VerificationLoginEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  @Index()
  verificationCode: string;

  @OneToOne(() => UserEntity)
  @JoinColumn({ referencedColumnName: 'id' })
  user: UserEntity;

  @Column({ type: 'timestamp' })
  verificationExpires: Date;

  @Column()
  ipRequests: string;

  @Column()
  browserRequests: string;

  @Column()
  countryRequests: string;
}

#

there are also strange errors like this

olive wind
#

Ah, I see it actually.

#

The AuthModule needs to forwardRef the CompanyMoudle

somber pelican
olive wind
#

AuthModule imports CompanyModule imports UsersMoudle imports AuthMoudle

#

The full import chain needs forwardRef

somber pelican
#

i see, means if it is still 1 chain then use forwardRef

somber pelican
olive wind
somber pelican
#

I have the same error but have used forwardRef, the code is like this

workspaceModule

import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { WorkspaceEntity } from './entities/workspace.entity';
import { CryptoModule } from '@/common/crypto/crypto.module';
import { WorkspaceService } from './workspace.service';
import { CompanyModule } from '@/company/company.module';
import { WorkspaceController } from './workspace.controller';
import { WorkspaceFlowModule } from './flow/flow.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([WorkspaceEntity]),
    CryptoModule,
    forwardRef(() => WorkspaceFlowModule),
    forwardRef(() => CompanyModule),
  ],
  controllers: [WorkspaceController],
  providers: [WorkspaceService],
  exports: [WorkspaceService],
})
export class WorkspaceModule {}

WorkspaceFlowModule

import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { WorkspaceFlowEntity } from './entities/flow.entity';
import { WorkspaceFlowService } from './flow.service';
import { WorkspaceFlowController } from './flow.controller';
import { BlockModule } from '@/block/block.module';
import { UsersModule } from '@/users/users.module';
import { WorkspaceModule } from '../workspace.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([WorkspaceFlowEntity]),
    BlockModule,
    UsersModule,
    forwardRef(() => WorkspaceModule),
  ],
  providers: [WorkspaceFlowService],
  controllers: [WorkspaceFlowController],
  exports: [WorkspaceFlowService],
})
export class WorkspaceFlowModule {}
olive wind
#

Is it an error from Nest or the IDE warning you?

somber pelican
# olive wind Is it an error from Nest or the IDE warning you?
Error: Nest cannot create the BlockModule instance.
The module at index [1] of the BlockModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> AuthModule -> UsersModule -> AuthModule -> WorkspaceModule -> WorkspaceFlowModule]
olive wind
#

So what is BlockModule?

somber pelican
# olive wind So what is `BlockModule`?
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BlockEntity } from './entities/block.entity';
import { BlockService } from './block.service';
import { BlockController } from './block.controller';
import { UsersModule } from '@/users/users.module';

@Module({
  imports: [TypeOrmModule.forFeature([BlockEntity]), UsersModule],
  controllers: [BlockController],
  providers: [BlockService],
  exports: [BlockService],
})
export class BlockModule {}
olive wind
#

Block imports users, users imports auth, auth imports workspace, workspace imports block. Circular chain

somber pelican
# olive wind Block imports users, users imports auth, auth imports workspace, workspace impor...

I found new error

Error: Nest cannot create the WorkspaceFlowModule instance.
The module at index [2] of the WorkspaceFlowModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [2] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule -> AuthModule -> UsersModule -> AuthModule -> WorkspaceModule]
olive wind
#

That seems to be the UsersModule.. Is it forwardReffed in the WorkspaceFlowModule?

somber pelican
# olive wind That seems to be the `UsersModule`.. Is it forwardReffed in the `WorkspaceFlowMo...
import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { WorkspaceFlowEntity } from './entities/flow.entity';
import { WorkspaceFlowService } from './flow.service';
import { WorkspaceFlowController } from './flow.controller';
import { BlockModule } from '@/block/block.module';
import { UsersModule } from '@/users/users.module';
import { WorkspaceModule } from '../workspace.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([WorkspaceFlowEntity]),
    BlockModule,
    forwardRef(() => UsersModule),
    forwardRef(() => WorkspaceModule),
  ],
  providers: [WorkspaceFlowService],
  controllers: [WorkspaceFlowController],
  exports: [WorkspaceFlowService],
})
export class WorkspaceFlowModule {}

already in flow

olive wind
#

Why is BlockModule not forward reffed?

somber pelican
#

because it does not require service from the workspace flow

import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BlockEntity } from './entities/block.entity';
import { BlockService } from './block.service';
import { BlockController } from './block.controller';
import { UsersModule } from '@/users/users.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([BlockEntity]),
    forwardRef(() => UsersModule),
  ],
  controllers: [BlockController],
  providers: [BlockService],
  exports: [BlockService],
})
export class BlockModule {}
olive wind
#

It's still part of that circular chain, right? Workspace to block to user to auth to workspace

somber pelican
#

I tried to give forwardRef but got a new error like this

Error: Nest can't resolve dependencies of the AuthService (UserEntityRepository, RefreshTokenEntityRepository, JwtService, ConfigService, CryptoService, ?, +, WorkspaceService). Please make sure that the argument dependency at index [5] is available in the AuthModule context.

Potential solutions:
- Is AuthModule a valid NestJS module?
- If dependency is a provider, is it part of the current AuthModule?
- If dependency is exported from a separate @Module, is that module imported within AuthModule?
  @Module({
    imports: [ /* the Module containing dependency */ ]
  })

I'm confused by chains like this

...

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      useFactory: (configService: ConfigService) => ({
        secret: configService.get('JWT_SECRET'),
        signOptions: {
          expiresIn: configService.get('JWT_EXPIRES_IN'),
          algorithm: 'HS512',
        },
      }),
      inject: [ConfigService],
    }),
    TypeOrmModule.forFeature([UserEntity, RefreshTokenEntity]),
    CryptoModule,
    forwardRef(() => UsersModule),
    forwardRef(() => CompanyModule),
    forwardRef(() => WorkspaceModule),
  ],
  controllers: [AuthController],
  providers: [AuthService, JwtStrategy],
  exports: [AuthService],
})
export class AuthModule {}
olive wind
#

That error is about your providers. They need forwardRefs too. @Inject(forwardRef(() => OtherDep)) private readonly service: OtherDep

olive wind
#

As a part of the service's constructor, yeah

#

And this will need to be done to each provider in the circular chain

somber pelican
somber pelican
olive wind
#

Luck

#

Nothing but pure luck

#

Circular imports are resolved in a non-deterministic manner, and it may work and it may not

somber pelican
#

I'm still confused

olive wind
#

What's got you confused?

somber pelican
olive wind
#

So, when a circular import happens, typescript and the node runtime have to make a decision about what to use in place of that value, otherwise the chain will never end and the program will stall taking up all the available memory until a fatal error happens.

So what happens is a dummy value is inserted and eventually "patched". Which value gets to be the dummy one is not known ahead of time

#

So, while on your machine it may work, it might not on a coworkers or in CI or production, because that dummy value may be interested elsewhere