Registry / database / connect2mongodb

connect2mongodb

JSON →
library1.1.68jsnpmunverified

Lightweight utility to dynamically connect and disconnect from a MongoDB database using mongoose. Current stable version 1.1.68. Designed for Express/Next.js servers, it simplifies connection management by reading MONGODB_URI or MONGO_URI from environment variables. Key differentiator: allows switching between a fixed database (URI includes db name) and a dynamic database (URI without db name, passed as argument). Requires a specific mongoose version (8.14.2) to avoid runtime errors. Low download volume and minimal maintenance.

npm install connect2mongodb
INSTALL
IMPORT
SIG · CONNECT2MONGODB
C
connect2mongodb
databasejavascriptv1.1.68
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.

connect2MongoDB
✓ import { connect2MongoDB } from 'connect2mongodb'
✗ const connect2MongoDB = require('connect2mongodb')
ESM-only package; no CommonJS support. Works with TypeScript.
disconnect2MongoDB
✓ import { disconnect2MongoDB } from 'connect2mongodb'
✗ import { disconnectDB } from 'connect2mongodb'
Correct function name is disconnect2MongoDB, not disconnectDB.
default import
✓ import * as connect2mongodb from 'connect2mongodb'; connect2mongodb.connect2MongoDB()
✗ import connect2mongodb from 'connect2mongodb'; connect2mongodb()
No default export; use named exports only.

Demonstrates two ways to connect: fixed database via MONGODB_URI or dynamic database via MONGO_URI plus database name argument.

// Ensure mongoose@8.14.2 is installed as a peer dependency import { connect2MongoDB, disconnect2MongoDB } from 'connect2mongodb'; // Method 1 (fixed database): set MONGODB_URI=mongodb+srv://user:pass@host/db in .env async function fixedDB() { try { const connected = await connect2MongoDB(); console.log('Connected:', connected); // ... use mongoose models const disconnected = await disconnect2MongoDB(); console.log('Disconnected:', disconnected); } catch (err) { console.error(err); } } // Method 2 (dynamic database): set MONGO_URI=mongodb+srv://user:pass@host/ in .env (no db name) async function dynamicDB() { try { const connected = await connect2MongoDB('my_database'); console.log('Connected:', connected); const disconnected = await disconnect2MongoDB(); console.log('Disconnected:', disconnected); } catch (err) { console.error(err); } } fixedDB(); dynamicDB();
Debug
Known issues
gotchaRequires exact mongoose version 8.14.2 – other versions cause errors when accessing DB after connection.
fix
Run: npm install mongoose@8.14.2
affects: >=0
gotchaEnvironment variables MONGODB_URI (fixed) and MONGO_URI (dynamic) are case-sensitive. Wrong key leads to connection failure.
fix
Use exactly MONGODB_URI for fixed database (includes db name) or MONGO_URI for dynamic database (no db name).
affects: >=0
gotchaThe package is ESM-only; require() will throw a runtime error.
fix
Use ES import syntax: import { connect2MongoDB } from 'connect2mongodb'
affects: >=0
breakingNo breaking changes documented; version is stable at 1.x.
deprecatedNo deprecations noted.
Errors
Common errors & fixes
TypeError: mongoose.model is not a function
Incompatible mongoose version installed (not 8.14.2).
fix
Uninstall current mongoose and install exactly 8.14.2: npm un mongoose && npm i mongoose@8.14.2
MongooseError: The uri parameter to mongoose.connect() must be a string, got undefined
Environment variables MONGODB_URI or MONGO_URI are missing or not loaded (dotenv not configured).
fix
Ensure .env file contains MONGODB_URI or MONGO_URI and import 'dotenv/config' at entry point.
SyntaxError: Cannot use import statement outside a module
Package is ESM-only; project not configured for ES modules.
fix
Add 'type': 'module' to package.json or convert file to .mjs extension.
Upgrade
Version history
1.1.68latest on npm
Audit
Dependencies
mongooserequiredPeer dependency - must be installed at exact version 8.14.2 or connections may fail
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources