Registry /
database / connect-mongodb-session
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.
MongoDBStore
✓ import pkg from 'connect-mongodb-session'; const MongoDBStore = pkg(session);
✗ import { MongoDBStore } from 'connect-mongodb-session';
Default export is a function that takes express-session and returns the store class.
MongoDBStore (require)
✓ const MongoDBStore = require('connect-mongodb-session')(session);
✗ const { MongoDBStore } = require('connect-mongodb-session');
CommonJS: require returns the function directly, must call with express-session.
session (express-session)
✓ import session from 'express-session';
✗ const session = require('express-session');
CommonJS require is acceptable but ESM import is preferred for modern projects.
Express session setup
✓ app.use(session({...options}));
✗ app.use(require('express-session')({...}));
Pass the middleware directly; do not call express-session inline incorrectly.
Sets up Express app with MongoDB session storage using connect-mongodb-session. Includes error handling and session view counter.
import express from 'express';
import session from 'express-session';
import connectMongo from 'connect-mongodb-session';
const MongoDBStore = connectMongo(session);
const app = express();
const store = new MongoDBStore({
uri: process.env.MONGO_URI ?? 'mongodb://localhost:27017/myapp',
databaseName: 'myapp',
collection: 'sessions'
});
store.on('error', (error) => console.error('Session store error:', error));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
store: store
}));
app.get('/', (req, res) => {
req.session.views = (req.session.views ?? 0) + 1;
res.send(`Views: ${req.session.views}`);
});
app.listen(3000, () => console.log('Server started on port 3000'));
Errors
Common errors & fixes
TypeError: MongoDBStore is not a constructor
Calling new MongoDBStore() directly on the default import without invoking the factory function with express-session.
fixconst MongoDBStore = require('connect-mongodb-session')(session); Error: Cannot find module 'connect-mongodb-session'
Package not installed or missing from node_modules.
fixnpm install connect-mongodb-session
MongoNetworkError: failed to connect to server on first connect
MongoDB connection string or server is unreachable.
fixCheck MongoDB URI and ensure the server is running.
The uri option is required
Missing 'uri' property in options object passed to MongoDBStore.
fixAdd uri: 'mongodb://localhost:27017/dbname' to the options.
Audit
Dependencies
express-sessionrequiredPeer dependency required to instantiate store
mongodbrequiredMongoDB driver for database connection