1

I am using nests framework and versions of mongodb and mongoose are as specified below. Please refer to the screenshot for error in detail.

versions

"mongodb": "4.0.0",
"mongoose": "5.5.12",

Error Screenshot

User Document Module

import { Module } from '@nestjs/common';
import { UserDocumentsService } from './user-documents.service';
import { UserDocumentsController } from './user-documents.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { UserDocumentsSchema } from './schema/user-documents.schema';

@Module({
    imports: [
        // showing error on this line
        MongooseModule.forFeature([
            { name: 'UserDocument', schema: UserDocumentsSchema },
        ]),
    ],
    controllers: [UserDocumentsController],
    providers: [UserDocumentsService],
})
export class UserDocumentsModule {}

App.module.ts

@Module({
imports: [
        MongooseModule.forRootAsync({
            imports: [SharedModule],
            useFactory: async (configService: ConfigService) => ({
                uri: configService.mongoDBName(),
                useNewUrlParser: true,
                useFindAndModify: false,
            }),
            inject: [ConfigService],
        }),
        UserDocumentsModule,
    ],
    providers: [AppGateway],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer): MiddlewareConsumer | void {
        consumer.apply(contextMiddleware).forRoutes('*');
    }
}

UPDATE I think there is something wrong with the mongoose imports in the schema file. It says "could not find declaration for module 'mongoose'".

Import error

I tried removing and reinstalling mongoose and it's types. But now it shows new error.

JavaScript heap out of memory

I tried solutions mentioned in this post: Node.js heap out of memory

But this also didn't work for me. I'm using Mac-M1 with 8GB config.

UPDATE The issue has been resolved now. The project is running on node v10.24.1 and I was using node v16.6.2. After downgrading node version using NVM, this issue is gone.

Sachin Yadav
  • 128
  • 3
  • 12

2 Answers2

0

You'll have to pull SharedModule import off MongooseModule.

Try this:

@Module({
    imports: [
        MongooseModule.forRootAsync({
            useFactory: async (configService: ConfigService) => ({
                uri: configService.mongoDBName(),
                useNewUrlParser: true,
                useFindAndModify: false,
            }),
            inject: [ConfigService],
        }),
        UserDocumentsModule,
        SharedModule
    ],
    providers: [AppGateway],
})

export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer): MiddlewareConsumer | void {
        consumer.apply(contextMiddleware).forRoutes('*');
    }
}
Paulo Fabrício
  • 319
  • 3
  • 17
0

It was because I was using a wrong version of node. The project was built on node v10.24.1 and I was using node v16.6.2.

After downgrading node version using NVM, I was able to fix this issue.

Sachin Yadav
  • 128
  • 3
  • 12