Registry / storage / connect-mongo

connect-mongo

JSON →
library6.0.0jsnpmunverified

MongoDB session store for Express and Connect written in TypeScript. Current stable version is 6.0.0, released with breaking changes from v4 rewritten in TypeScript. Supports Express 4.x/5.0, Connect 3.x, and MongoDB Node driver 5.x-7.x, with CI testing across Node 20, 22, and 24. Key differentiator is seamless integration with express-session providing MongoDB-backed session persistence, lazy session updates, transparent encryption, and configurable auto-removal of expired sessions. Requires express-session and mongodb as peer dependencies.

npm install connect-mongo
INSTALL
IMPORT
SIG · CONNECT-MONGO
C
connect-mongo
storagejavascriptv6.0.0
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.

MongoStore
import MongoStore from 'connect-mongo'
const MongoStore = require('connect-mongo')
Default ESM import. Also works with CJS require but TypeScript types require default import.
MongoStore.create
MongoStore.create(options)
new MongoStore(options)
In v4+, use factory method .create() instead of constructor. Constructor is deprecated and throws.

Basic Express session setup using MongoStore.create() with MongoDB connection string, TTL, native auto-removal, and lazy session update.

import session from 'express-session'; import MongoStore from 'connect-mongo'; app.use(session({ secret: process.env.SESSION_SECRET ?? 'keyboard cat', resave: false, saveUninitialized: true, store: MongoStore.create({ mongoUrl: process.env.MONGO_URL ?? 'mongodb://localhost:27017/sessions', collectionName: 'sessions', ttl: 14 * 24 * 60 * 60, // 14 days autoRemove: 'native', touchAfter: 24 * 3600 // lazy update only if 24 hours have passed }) }))
Debug
Known issues
breakingv4 drops support for Node < 20.8 and MongoDB driver < 5.0
fix
Upgrade Node to >=20.8.0, mongodb driver to >=5.0.0
affects: >=4.0.0 <5.0.0
breakingConstructor `new MongoStore()` removed in v4; use factory method `MongoStore.create()`
fix
Replace `new MongoStore(options)` with `MongoStore.create(options)`
affects: >=4.0.0
deprecated`autoRemoveInterval` option deprecated in v4; use `autoRemove` instead
fix
Use `autoRemove: 'interval'` and `autoRemoveInterval: value` or `autoRemove: 'native'`
affects: >=4.0.0
gotchaCalling MongoStore.create() without session middleware causes undefined errors
fix
Ensure MongoStore.create() is called inside a valid Express/Connect app with session middleware
affects: >=4.0.0
gotchacircular dependency in MongoError exports when using certain bundlers
fix
Use direct import of MongoStore instead of destructured { MongoStore }
affects: >=4.0.0
Errors
Common errors & fixes
MongoError: Authentication failed.
Invalid credentials or wrong authSource
fix
Ensure mongoUrl includes correct username, password, and authSource (e.g., ?authSource=admin)
MongoStore.create is not a function
Using CommonJS require with default import
fix
Use `const MongoStore = require('connect-mongo').default` or switch to ESM import
TypeError: Cannot read properties of undefined (reading 'collection')
MongoStore instance used before connection is established
fix
Wait for the mongoose connection? Or use mongoUrl option to let connect-mongo manage the connection
Upgrade
Version history
6.0.0latest on npm
Audit
Dependencies
express-sessionoptionalPeer dependency required for session middleware
mongodboptionalPeer dependency required for MongoDB driver
Agent activity
22 hits · last 30 days
node
22
Resources
connect-mongo — npm install connect-mongo · libregistry