while running node app.js i was facing this problem. I was not able to solve it
throw new MongoParseError('mongodb+srv URI cannot have port number');
^
MongoParseError: mongodb+srv URI cannot have port number
at new ConnectionString (/Users/austin/Desktop/FruitsProject/node_modules/mongodb-connection-string-url/lib/index.js:146:23)
at parseOptions (/Users/austin/Desktop/FruitsProject/node_modules/mongodb/lib/connection_string.js:201:17)
at new MongoClient (/Users/austin/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:46:63)
at Object.anonymous (/Users/austin/Desktop/FruitsProject/app.js:8:16)
at Module.compile (node:internal/modules/cjs/loader:1218:14)
at Module.extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module.load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint as runMain (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47`
When you use DNS Seed List Connection Format (the connection string prefix with
mongodb+srv
), the connection string URI should not contains a port number.For example:
DNS Seed List Connection Format:
Standard Connection String Format:
The MongoDB nodejs driver use mongodb-connection-string-url module to handle the connection string URI.
It will validate the URI, see mongodb-js/mongodb-connection-string-url/blob/v2.6.0/src/index.ts#L201
The below code will throw the error because it contains a port number:
runkit