Receive DEP0170 when connecting MongoDB Atlas with Node.js 20.3.1
Asked Answered
M

5

8

Recently I updated Node.js to version 20.3.1 and tried to connect my MongoDB Atlas. When my Node.js's version is 19.X, the connection of Atlas is normal. However, The terminal shows the deprecation warning in the Node.js 20.3.1:

[DEP0170] DeprecationWarning: The URL mongodb://:@ac-4hlp44x-shard-00-01.nqjamzn.mongodb.net:27017,ac-4hlp44x-shard-00-02.nqjamzn.mongodb.net:27017,ac-4hlp44x-shard-00-00.nqjamzn.mongodb.net:27017/?authSource=admin&replicaSet=atlas-g8a2vr-shard-0&retryWrites=true&w=majority&ssl=true is invalid. Future versions of Node.js will throw an error.

The original URL for connecting to Atlas is:

MONGODB_URI=mongodb+srv://<USERNAME>:<PASSWORD>@<USERNAME>.nqjamzn.mongodb.net/<DATABASE>?retryWrites=true&w=majority

The database is still connected.

I tried to search the related articles, but there's not any information about this deprecation.

How can I fix it?

Mcelroy answered 1/7, 2023 at 11:46 Comment(1)
Node 20 doesn't think mongodb atlas url is valid. There is nothing you can do other than waiting either mongodb or nodejs team fix this.Embolic
R
5

i had the same problem this is caused by old version of mongoose just update mongoose and it well be solved

npm install mongoose@latest
Rogerrogerio answered 22/1 at 11:45 Comment(0)
C
4

I am seeing the same thing and looking at the node depreciation docs (https://nodejs.org/api/deprecations.html#DEP0170), it implies that it doesn't like non-number ports. I think therefore as the Mongo Atlas connection string tends to contain lots of ':' characters (e.g. mongodb+srv://<USERNAME>:<PASSWORD>), it thinks that these are supposed to be port numbers.

Not sure on the resolution yet but hopefully this helps.

Circumjacent answered 3/7, 2023 at 15:6 Comment(2)
answer that are easy to read and understand usually earn more points. Please add more info and structure your answer using the instruction on the column on the rightLysimeter
It's pretty clear from his answer what the problems is Ion. DEP0170: Invalid port when using url.parse()# History Type: Runtime url.parse() accepts URLs with ports that are not numbers. This behavior might result in host name spoofing with unexpected input. These URLs will throw an error in future versions of Node.js, as the WHATWG URL API does already.Gilliangilliard
C
0

To fix Mongoose connection errors:

  • Update Mongoose to the latest version: npm install mongoose@latest
  • Remove these options from mongoose.connect:
    • useCreateIndex
    • useFindAndModify
    • useNewUrlParser
    • useUnifiedTopology

That's what I did and it's working just fine now.

Chunk answered 18/5 at 15:12 Comment(0)
L
0

Here is what worked for me.

(1) Update mongoose to the latest version.

npm install mongoose@latest

(2) Remove the meta options from the mongoose connect function.

// db
mongoose
  .connect(process.env.ATLAS_URI)
  .then(() => console.log("Connected to MongoDB"))
  .catch((err) => console.log(`DB connection error - ${err}`));
Lichen answered 11/7 at 4:55 Comment(0)
R
-1

This happens because the "sharded URL" you get for MongoDB Atlas isn't recognised as a valid URL by Node 20 (presumably due to the new parser version referred to in the release notes).

Per e.g. https://jira.mongodb.org/browse/NODE-5802 (emphasis mine):

The 3.x branch of the driver will throw a deprecation warning when run in Node 20 (https://nodejs.org/api/deprecations.html#DEP0170)

...

Note this does not affect 4.x+ of the driver.

you need to ensure that the installed version of the MongoDB driver (see this with e.g. npm ls mongodb or yarn list --pattern mongo) is at least v4. If the drivers are installed via Mongoose, this should be at least v6.

These newer versions do not directly pass a sharded URL to URL, so don't have the same problem.

Rochellerochemont answered 2/5 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.