An issue with Nestjs mongoose module after installation

Viewed 4502

I installed @nestjs/mongoose with following npm command:

npm i --save @nestjs/mongoose mongoose

But when I try to lauch the app, I get following errors:

node_modules/@nestjs/mongoose/dist/decorators/prop.decorator.d.ts:2:44 - error TS2694: Namespace '"mongoose"' has no exported member 'SchemaTypeOpts'.

export declare type PropOptions = mongoose.SchemaTypeOpts<any> | mongoose.Schema | mongoose.SchemaType;
                                         ~~~~~~~~~~~~~~
node_modules/@nestjs/mongoose/dist/factories/schema.factory.d.ts:4:60 - error TS2315: Type 'Schema' is not generic.

static createForClass<T = any>(target: Type<unknown>): mongoose.Schema<T>;
                                                         ~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/mongoose/dist/interfaces/mongoose-options.interface.d.ts:3:10 - error TS2305: Module '"mongoose"' has no exported member 'ConnectionOptions'.

import { ConnectionOptions } from 'mongoose';
       ~~~~~~~~~~~~~~~~~

Found 3 error(s).

Any help would be highly appreciated.

4 Answers

Revert your mongoose install down to 5.10.x. The latest version of mongoose came out with their own set of types, which ended up breaking a lot of things: Reference Issue

The best combination that worked for me is the following.

"@nestjs/mongoose": "^7.0.4",
"mongoose": "^5.10.19",
"@types/mongoose": "^5.10.3", //dev-dependency

As of 19 Dec 2020, any version later than that did NOT work for me.

As mentioned here, the best thing to do is to upgrade:

"@nestjs/mongoose": "^7.2.0",
"mongoose": "^5.11.5",

It might be better to remove those dependencies and run npm install --save @nestjs/mongoose mongoose again

Also remove @types/mongoose": ^5.10.2" if you have that.

Since mongoose v5.11.x, the module exposes it's own file definition and can break your build!

To solve it, install @tsed/mongoose and remove the @types/mongoose dependencies.

Run the following commands:

npm i --save @nestjs/mongoose mongoose
npm i --save-dev @tsed/mongoose

And make sure you've removed @types/mongoose

Related