{"id":13571,"library":"mongodb-uri","title":"MongoDB URI Parser and Formatter","description":"The `mongodb-uri` library provides utilities for parsing and formatting MongoDB connection URIs in Node.js environments. It takes a URI string and converts it into a structured JavaScript object, and vice-versa, allowing programmatic manipulation of connection parameters. A key differentiator is its `formatMongoose` function, designed to transform multi-host MongoDB URIs into a format compatible with Mongoose's `connect()` method, which expects individual host/port pairs. This is particularly useful for environments like PaaS providers (e.g., Heroku with MongoLab) where a single URI might need adaptation. The current stable version is 0.9.7, indicating a pre-1.0 status, and the package has seen no updates since early 2017, suggesting it is no longer actively maintained. It handles URL encoding for special characters within usernames, passwords, and database names.","status":"abandoned","version":"0.9.7","language":"javascript","source_language":"en","source_url":"https://github.com/mongolab/mongodb-uri-node","tags":["javascript","mongodb","uri","url","parser","formatter","mongoose"],"install":[{"cmd":"npm install mongodb-uri","lang":"bash","label":"npm"},{"cmd":"yarn add mongodb-uri","lang":"bash","label":"yarn"},{"cmd":"pnpm add mongodb-uri","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only, reflecting its age. There are no ESM exports.","symbol":"mongodbUri","correct":"const mongodbUri = require('mongodb-uri');"},{"note":"Named destructuring for specific functions is common, but the primary export is the module object.","symbol":"parse","correct":"const { parse } = require('mongodb-uri');"},{"note":"This specific function is for adapting URIs for Mongoose's connection string format.","symbol":"formatMongoose","correct":"const { formatMongoose } = require('mongodb-uri');"}],"quickstart":{"code":"const mongodbUri = require('mongodb-uri');\n\nconst uri = 'mongodb://user%3An%40me:p%40ssword@host:1234,host2:5678/d%40tabase?authSource=%40dmin&replicaSet=myReplicaSet';\n\n// Parse the URI into an object\nconst uriObject = mongodbUri.parse(uri);\nconsole.log('Parsed URI Object:');\nconsole.log(JSON.stringify(uriObject, null, 2));\n\n// Format the object back into a URI string\nconst formattedUri = mongodbUri.format(uriObject);\nconsole.log('\\nFormatted URI String:');\nconsole.log(formattedUri);\n\n// Example for Mongoose compatibility (if multiple hosts are present)\nconst mongooseConnectString = mongodbUri.formatMongoose(uri);\nconsole.log('\\nMongoose Compatible URI String:');\nconsole.log(mongooseConnectString);","lang":"javascript","description":"Demonstrates parsing a MongoDB URI string, formatting a URI object back to a string, and generating a Mongoose-compatible connection string from a multi-host URI."},"warnings":[{"fix":"Consider using the native MongoDB Node.js driver's `MongoClient` connection string parsing or actively maintained alternatives like `mongodb-url` or `mongo-uri-builder` if modern MongoDB features are required.","message":"The package has not been updated since January 2017 (version 0.9.7). It may not support newer MongoDB URI features, options, or authentication mechanisms (e.g., `mongodb+srv` SRV records) introduced in modern MongoDB versions and drivers.","severity":"breaking","affected_versions":"<=0.9.7"},{"fix":"Always test parsing results against expected standard MongoDB URI structures, especially for edge cases involving missing passwords or database paths.","message":"The library notes two minor differences from the standard MongoDB connection string URI format: passwords are optional even with a username, and the slash before the database is not required when specifying options without a database. While this might not prevent parsing standard URIs, it highlights potential deviations.","severity":"gotcha","affected_versions":">=0.6.0"},{"fix":"For ESM projects, use `const mongodbUri = require('mongodb-uri');` or consider transpilation/bundling. Alternatively, use a modern alternative that supports ESM.","message":"This package is CommonJS-only and does not provide ESM exports. This means it cannot be directly imported using `import ... from 'mongodb-uri'` in pure ESM Node.js projects, requiring `require()` or bundling solutions.","severity":"gotcha","affected_versions":">=0.6.0"},{"fix":"Avoid using this package for MongoDB URIs involving IPv6 addresses, or manually parse and format IPv6 host portions if absolutely necessary.","message":"The package does not inherently handle IPv6 addresses, which has been reported as an open issue.","severity":"gotcha","affected_versions":">=0.6.0"}],"env_vars":null,"search_vec":"'-1.0':104 '0.9.7':100 '2017':115 'activ':121 'adapt':94 'allow':40 'charact':129 'compat':64 'connect':18,44,68 'convert':29 'current':96 'databas':134 'design':53 'differenti':48 'e.g':84 'earli':114 'encod':126 'environ':22,80 'expect':71 'format':16,63 'formatmongoos':51 'formatt':5,141 'function':52 'handl':124 'heroku':85 'host':58 'host/port':73 'indic':101 'individu':72 'javascript':34,136 'key':47 'librari':10 'like':81 'longer':120 'maintain':122 'manipul':42 'method':69 'might':92 'mongodb':1,8,17,59,137 'mongodb-uri':7 'mongolab':87 'mongoos':66,142 'multi':57 'multi-host':56 'name':135 'need':93 'node.js':21 'object':35 'paa':82 'packag':108 'pair':74 'paramet':45 'pars':14 'parser':3,140 'particular':77 'password':132 'pre':103 'programmat':41 'provid':11,83 'seen':110 'sinc':113 'singl':90 'special':128 'stabl':97 'status':105 'string':27 'structur':33 'suggest':116 'take':24 'transform':55 'updat':112 'uri':2,9,19,26,60,91,138 'url':125,139 'use':78 'usernam':131 'util':12 'versa':39 'version':98 'vice':38 'vice-versa':37 'within':130","created_at":"2026-04-20T01:55:11.197730+00:00","updated_at":"2026-04-20T01:55:11.197730+00:00","problems":[{"fix":"Ensure the URI variable is a valid string before passing it to `mongodbUri.parse()` or `mongodbUri.formatMongoose()`. For example, `const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/test';`","cause":"Input URI string is `undefined` or `null` when passed to `parse` or `formatMongoose`.","error":"TypeError: Cannot read properties of undefined (reading 'split')"},{"fix":"Review the URI format against the MongoDB connection string specification. Ensure special characters in username, password, or database name are properly URL-encoded (e.g., `@` becomes `%40`, `:` becomes `%3A`).","cause":"The input string does not conform to the expected MongoDB URI format or contains unescaped special characters.","error":"Error: Invalid URI: '...'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/mongolab/mongodb-uri-node","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/mongodb-uri","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}