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.fixRun: 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.fixUse 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.fixUse 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).
fixUninstall 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).
fixEnsure .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.
fixAdd 'type': 'module' to package.json or convert file to .mjs extension.
Audit
Dependencies
mongooserequiredPeer dependency - must be installed at exact version 8.14.2 or connections may fail