1

Getting this very well known warning :

(node:15304) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

The usual fix is to use a Promise library like Bluebird instead :

mongoose.Promise = require('bluebird');

(Edit) or mongoose.Promise = global.Promise, but according to this post and this post, Bluebird is 4 times faster than ES6's native Promise

However, in Nest, we're not including Mongoose directly. We include Nest's own @nestjs/mongoose module (which internally, imports Mongoose).

import { MongooseModule } from '@nestjs/mongoose';

@Module({
    controllers: [AdsController],
    imports: [ MongooseModule.forRoot('mongodb://localhost/nest') ]
})

Therefore mongoose is undefined so I can't attach Bluebird to it.

How to inject Bluebird in Mongoose when using NestJS?

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • What do you mean "the usual fix"? If you read blog posts from 2010 maybe. Try `mongoose.Promise = global.Promise` in most "modern" nodejs environments which support promises right out of the box. – Neil Lunn Apr 28 '18 at 09:31
  • 2
    and `import * as mongoose from 'mongoose'`. You only need to set the promise library once in the whole application code. nodejs will only import modules once. – Neil Lunn Apr 28 '18 at 09:33
  • If you refer to [this post](https://stackoverflow.com/questions/38138445/node3341-deprecationwarning-mongoose-mpromise), using Bluebird would be 4 times faster than `mongoose.Promise = global.Promise` – Jeremy Thille Apr 28 '18 at 09:33
  • 1
    @NeilLunn, apparently that did the trick. I imported `mongoose`, then defined `mongoose.Promise`, then imported `@nestjs/mongoose` and the deprecation warning disappeared. If you write that as an answer, I'll mark it as accepted. Thanks :) – Jeremy Thille Apr 28 '18 at 09:38
  • and [another thread for you to read](https://softwareengineering.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than-bluebird). – Jeremy Thille Apr 28 '18 at 09:42
  • @NeilLunn please consider posting your comment as an answer. – Benjamin Gruenbaum Apr 28 '18 at 19:48
  • @JeremyThille by the way, bluebird is now ~1.5 times faster than native promises - especially when using the builtin promise APIs. There is work done on making native promises faster in Node. If you have any specific use case you'd like to share with us we'd love to hear it. (disclosure, I'm a core member in both bluebird and Node). – Benjamin Gruenbaum Apr 28 '18 at 19:50
  • No real specific use case - I'm just experimenting and trying to implement Mongoose in Nest, and oddly enough, it's way harder than I expected – Jeremy Thille Apr 29 '18 at 09:10

0 Answers0