Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true
Asked 07 September, 2021
Viewed 610 times
  • 54
Votes

I have a database wrapper class that establishes a connection to some MongoDB instance:

async connect(connectionString: string): Promise<void> {
        this.client = await MongoClient.connect(connectionString)
        this.db = this.client.db()
}

This gave me a warning:


  

(node:4833) 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.

The connect() method accepts a MongoClientOptions instance as second argument. But it doesn't have a property called useNewUrlParser. I also tried to set those property in the connection string like this: mongodb://127.0.0.1/my-db?useNewUrlParser=true but it has no effect on those warning.

So how can I set useNewUrlParser to remove those warning? This is important to me since the script should run as cron and those warnings result in trash-mail spam.

I'm using mongodb driver in version 3.1.0-beta4 with corresponding @types/mongodb package in 3.0.18. Both of them are the latest avaliable using npm install.

Workaround

Using an older version of mongodb driver:

"mongodb": "~3.0.8",
"@types/mongodb": "~3.0.18"

22 Answer