2

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

I am new to nest and am facing this issue, the app.controller.ts is

import { Module } from '@nestjs/common';
import { ItemsController } from './items.controller';
import { ItemsService } from './items.service';
import { MongooseModule } from '@nestjs/mongoose';
import { ItemSchema } from './schema/item.schema';

@Module({
  imports: [MongooseModule.forFeature([{name:'Item',schema:ItemSchema}])],
  controllers: [ItemsController],
  providers: [ItemsService],
})
export class ItemsModule {}

Any help would be appreciated

Ldr
  • 31
  • 2
  • 5
  • 1
    this is likely due to multiple modules loaded for the same `@nestjs/core` package. If you're in a monorepo, see this: https://github.com/nestjs/docs.nestjs.com/pull/2152/files – Micael Levi Feb 03 '22 at 23:18
  • I think my error is first one, how to resolve that? – Ldr Feb 03 '22 at 23:20
  • not having `@nestjs/core` as hard dependency in your side. Without context, it's hard to tell why you end up with many `@nestjs/core`. – Micael Levi Feb 04 '22 at 00:49

3 Answers3

1

In you case, you can try to add this to your ItemSchema :

// schema/item.schema

@Schema({ collection: 'items' })

And make sure the import with a plural name :

// items.module.ts

// [...]
@Module({
  imports: [MongooseModule.forFeature([{name:'items',schema:ItemSchema}])],
  controllers: [ItemsController],
  providers: [ItemsService],
})
export class ItemsModule {}

According to this question.

Or if you prefer to keep the name schema name item, you can force it by following this answer

Mongoose is trying to be smart by making your collection name plural. You can however force it to be whatever you want:

var dataSchema = new Schema({..}, { collection: 'data' })

rcarette
  • 111
  • 2
  • 10
0

This error occurs when you dont start your code in the proper directory in your program folder. So cd navigate to the directory of your project and and run all the codes again.

Henry2
  • 381
  • 3
  • 3
0

I faced this error while i am learning Connecting Nest.js with Azure Cosmos DB. Error : "Nest can't resolve dependencies of the AzureCosmosDbCoreModule (COSMOS_DB_CONNECTION_NAME, ?). Please make sure that the argument ModuleRef at index [1] is available in the AzureCosmosDbCoreModule context.

Potential solutions:
- Is AzureCosmosDbCoreModule a valid NestJS module?
- If ModuleRef is a provider, is it part of the current AzureCosmosDbCoreModule?
- If ModuleRef is exported from a separate @Module, is that module imported within AzureCosmosDbCoreModule?
  @Module({
    imports: [ /* the Module containing ModuleRef */ ]
  })"
Here is the solution: 
Check your dependencies in the Package.json file.

"dependencies": {
    "@azure/cosmos": "^3.17.3",
    "@nestjs/azure-database": "^2.3.0",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "dotenv": "^16.1.4",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "overrides": {
    "@nestjs/azure-database": {
      "@nestjs/common": "^9.1.4",
      "@nestjs/core": "^9.1.4"
    } 
This may help you!.