Kishan Donga
Kishan Donga

Reputation: 3193

Nest can't resolve dependencies of the TypeOrmCoreModule

src/contact/contact.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ContactController } from './contact.controller';
import { Contact } from './contact.entity';
import { ContactRepository } from './contact.repo';
import { ContactService } from './contact.service';

@Module({
  imports: [
     TypeOrmModule.forFeature([
      Contact,
    ]), 
  ],
  controllers: [ContactController],
  providers: [ContactService, ContactRepository],
})
export class ContactModule {}

src/app.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { getMetadataArgsStorage } from 'typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ContactModule } from './contact/contact.module';

@Module({
  imports: [
   TypeOrmModule.forRoot({
      type: 'sqlite',
      database: 'db',
      entities: getMetadataArgsStorage().tables.map(tbl => tbl.target),
      synchronize: true,
    }), 
    ContactModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

npm new start:dev

Give this kind of error where I try to find possibly every solution on the internet but what mistake I'm doing I don't know, I got an error like.

nest -v (8.1.6)

ERROR [ExceptionHandler] Nest can't resolve dependencies of the TypeOrmCoreModule (TypeOrmModuleOptions, ?). Please make sure that the argument ModuleRef at index [1] is available in the TypeOrmCoreModule context.

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

Upvotes: 6

Views: 14192

Answers (3)

Leart Beqiraj
Leart Beqiraj

Reputation: 21

Make sure you have same versions across different packages of a monorepo project, specifically below libraries:

"@nestjs/common": "^10.3.7",
"@nestjs/core": "^10.3.7",
"@nestjs/platform-express": "^10.3.7",

Upvotes: 1

Yogesh Umesh Vaity
Yogesh Umesh Vaity

Reputation: 48089

I had this error for TypeOrmCoreModule when I switched from the globally available ConfigService to a custom EnvConfigService. In that case, for example, I needed to import the module in which the custom EnvConfigService resided; as shown in the bold statement below:


    const typeOrmModule = TypeOrmModule.forRootAsync({
      imports: [EnvConfigModule],
      inject: [EnvConfigService],
      useFactory: (config: EnvConfigService) => {
        return {
          type: 'postgres',
          host: config.postgresHost,
          port: config.postgresPort,
          database: config.postgresDatabaseName,
          username: config.postgresUsername,
          password: config.postgresPassword,
          synchronize: true,
          entities: [User, PasswordRecovery]
        }
      }
    })

The import statement was not required when I injected ConfigService because it's globally accessible.

Upvotes: 0

Alyf Mendonça
Alyf Mendonça

Reputation: 151

I was having this problem because of a bad installation of typeorm.

Works for me:

1- Delete node_module
2- Run npm i
3- Run npm install --save @nestjs/typeorm typeorm mysql2 again im my project.

Upvotes: 9

Related Questions