DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated with 4.8.0 and 4.8.1
Problem
With mongoose versions 4.8.0 and 4.8.1 (node version is 6.9.5 and MongoDB version is 3.2.11) the following warning is generated the first time the save of a document occurs after starting the application (the warning does not appear when the same code is executed subsequent times after the first time while the application is still running, however, if the application is closed and restarted, then the first time a document save occurs after the application has been started the warning is generated): > DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html This issue does not appear when using the exact same application code with mongoose versions 4.7.9 and earlier. The code that sets up the mongoose connection is as follows: `// Set up mongoose and DB connection var mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); mongoose.connect('mongodb://localhost:27017/basedir', {server: { poolSize: 5 }});`
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Replace Deprecated mpromise with Bluebird in Mongoose Setup
The warning occurs because Mongoose versions 4.8.0 and 4.8.1 have deprecated their default promise library, mpromise. The application is still using the default promise library instead of an alternative like Bluebird, which is explicitly set in the code. This leads to the deprecation warning being triggered on the first document save after starting the application.
Awaiting Verification
Be the first to verify this fix
- 1
Install Bluebird
Ensure that Bluebird is installed in your project as it will be used as the promise library for Mongoose.
bashnpm install bluebird - 2
Update Mongoose Connection Code
Modify the Mongoose connection setup to explicitly set Bluebird as the promise library before connecting to the database.
javascriptvar mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); mongoose.connect('mongodb://localhost:27017/basedir', {server: { poolSize: 5 }}); - 3
Test Document Save
After making the changes, run your application and attempt to save a document to the database. This will help confirm that the deprecation warning no longer appears.
javascriptconst MyModel = mongoose.model('MyModel', new mongoose.Schema({ name: String })); const doc = new MyModel({ name: 'Test' }); doc.save(); - 4
Check for Deprecation Warning
Observe the console output during the first document save after restarting the application. Ensure that the DeprecationWarning does not appear.
bashNo code required
Validation
Confirm that the application runs without the DeprecationWarning during the first document save after a restart. Additionally, check that all functionalities relying on Mongoose promises work as expected.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep