Registry / database / mongodb-url

mongodb-url

JSON →
library3.0.3jsnpmunverified

Parses MongoDB connection strings into structured objects. v3.0.3, stable but low maintenance. Differentiates from mongoose's parser by being standalone and simple, supporting both mongodb:// scheme and host-only formats. Returns {dbName, servers} with optional authentication and options fields. Ideal for CLI tools and lightweight MongoDB connection setups without full driver dependencies.

npm install mongodb-url
INSTALL
IMPORT
SIG · MONGODB-URL
M
mongodb-url
databasejavascriptv3.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import parse from 'mongodb-url'
import { parse } from 'mongodb-url'
Default export only; named import will fail.
parse
const parse = require('mongodb-url')
const { parse } = require('mongodb-url')
CommonJS: default export assigned to module.exports.
format
import { format } from 'mongodb-url'
Named export available in v3+; formats a config object back to a URL string.
parseAuth
import { parseAuth } from 'mongodb-url'
Named export for parsing authentication parts only.

Demonstrates basic parsing of host:port, full URI with auth, and formatting config back to URL string.

import parse from 'mongodb-url'; import { format } from 'mongodb-url'; // Parse a simple host:port const result1 = parse('localhost:27017'); console.log(result1); // { dbName: 'admin', servers: [ { host: 'localhost', port: 27017 } ] } // Parse a full MongoDB URI const result2 = parse('mongodb://user:pass@host1:27017,host2:27018/mydb?replicaSet=rs0'); console.log(result2); // { servers: [ { host: 'host1', port: 27017 }, { host: 'host2', port: 27018 } ], // dbName: 'mydb', // auth: { username: 'user', password: 'pass' }, // options: { replicaSet: 'rs0' } } // Format back to a URL const config = { servers: [{ host: 'localhost', port: 27017 }], dbName: 'test', options: { ssl: true } }; const url = format(config); console.log(url); // mongodb://localhost:27017/test?ssl=true
Debug
Known issues
breakingv3.0 moved from default export { parse } to default export parse function. Code using `import { parse } from 'mongodb-url'` will break.
fix
Change to `import parse from 'mongodb-url'` or use named import `{ parse }` (named still works? Check docs). Actually v3 renamed the default export to parse, removing named 'parse'? From README: 'var parse = require('mongodb-url')' suggests default export is parse. Named 'parse' may not exist. Safer: use default import.
affects: <3.0.0
deprecatedThe `options` object in parsed output may include deprecated fields like `w`, `j`, `wtimeoutMS` that are MongoDB driver specific. Prefer using the driver's own URI options.
fix
Filter options to only standard connection string options before passing to MongoClient.
affects: >=3.0.0
gotchaParsing a URI without specifying a database defaults `dbName` to `'admin'`. This is often unintended; explicitly set /dbname in URI.
fix
Always include a database name in the URI, e.g., `mongodb://localhost:27017/mydb`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mongodb_url_1.default is not a function
Using default import when the package only exports a named function (in older versions). Or importing with wrong syntax.
fix
Use `import parse from 'mongodb-url'` (v3+) or `const parse = require('mongodb-url')` for CommonJS.
Cannot find module 'mongodb-url'
Package not installed or using an old npm registry that doesn't have it.
fix
Run `npm install mongodb-url` and check that package.json includes it.
data.dbName must be a string
Passing a non-string or undefined value to format() for dbName.
fix
Ensure dbName is a string (default 'admin' if omitted, but explicitly set it).
Upgrade
Version history
3.0.3latest on npm
Audit
Dependencies
mongodboptionalpeer dependency used for connection validation (not required for parsing)
Agent activity
2 hits · last 30 days
node
2
Resources
mongodb-url — npm install mongodb-url · libregistry