[Mongoose] How to Fix (node:42286) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Tadashi Shigeoka · Sat, December 21, 2019
I’ll introduce how to fix the error (node:42286) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. that occurs in web applications with Express (Node.js) + Mongoose (MongoDB) configuration.
Solution: Add { useNewUrlParser: true } Option
- Official documentation: Mongoose v5.8.3: Connecting to MongoDB
(node:42286) DeprecationWarning Error Message
(node:42286) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Sample Code Before Fixing (node:42286) DeprecationWarning
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URL);
Sample Code After Fixing (node:42286) DeprecationWarning
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true });
That’s all from the Gemba.