MongoDB Memory Server Global 4.4
mongodb-memory-server-global-4.4 provides an isolated, in-memory MongoDB server instance specifically configured for MongoDB version 4.4, designed for automated testing environments. It automatically downloads the MongoDB 4.4 binary to a local cache directory upon installation, eliminating the need for a globally installed MongoDB. As of early 2025, the package is at version 11.0.1, part of an actively maintained project that sees frequent patch and minor releases, with major versions released periodically to align with Node.js and MongoDB version updates. Its key differentiator is the ease of use and version pinning for testing, contrasting with the base `mongodb-memory-server` package which requires explicit version configuration or defaults to a newer MongoDB release. This global package ensures consistent test environments tied to MongoDB 4.4.
Common errors
-
Error: MongoDB binary for version X.Y.Z is not supported on this platform.
cause The specific MongoDB binary version you are trying to use (or the default set by a package) is not compatible with your operating system or CPU architecture, or it's a version that has been explicitly deprecated by `mongodb-memory-server`.fixEnsure your system is running a supported architecture (e.g., not an unsupported older macOS/ARM combination for very old MongoDB versions). If you are overriding the version, specify a supported version (e.g., `await MongoMemoryServer.create({ version: '4.4.0' })`). For this package, the version is fixed at 4.4. -
Error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:xxxxx
cause The MongoDB Memory Server instance failed to start correctly, crashed, or was stopped before the client attempted to connect. This can be due to port conflicts, insufficient permissions, or issues with the downloaded binary.fixVerify that no other process is using the allocated port (the server tries to find a free port automatically, but conflicts can occur). Check the console output for any error messages during `MongoMemoryServer.create()`. Ensure you are awaiting `create()` before attempting to connect a client. Consider adding `await mongoServer.stop()` in a `finally` block or `afterAll` hook to prevent resource leaks. -
TypeError: Cannot read properties of undefined (reading 'connect')
cause This typically occurs when trying to use a MongoDB client (like `MongoClient` or `mongoose`) before the `mongoUri` is properly obtained from a *successfully started* `MongoMemoryServer` instance.fixEnsure that `MongoMemoryServer.create()` successfully resolves and that `mongoServer.getUri()` is called *after* the server is confirmed to be running. Always handle potential errors from `create()` and subsequent operations.
Warnings
- breaking mongodb-memory-server-global-4.4 version 11.0.0 and above requires Node.js version 20.19.0 or higher. Older Node.js environments are no longer supported.
- breaking When compiling TypeScript, `mongodb-memory-server-global-4.4` version 11.0.0 upgrades its internal `tsconfig` target to `es2023`. This might cause compilation issues if your project's `tsconfig.json` targets an older ES version or has incompatible settings.
- gotcha While this specific package (`-global-4.4`) is designed for MongoDB 4.4, the underlying `mongodb-memory-server` library (from which this package re-exports) dropped support for MongoDB versions below 4.2.0 in v11.0.0. Attempting to manually configure this package to an unsupported older MongoDB version will fail.
- deprecated The separate package `mongodb-memory-server-global-4.0` was removed in the v11.0.0 release of the `mongodb-memory-server` ecosystem, as it is no longer supported by the latest MongoDB drivers. Users of that package should upgrade.
Install
-
npm install mongodb-memory-server-global-4.4 -
yarn add mongodb-memory-server-global-4.4 -
pnpm add mongodb-memory-server-global-4.4
Imports
- MongoMemoryServer
import MongoMemoryServer from 'mongodb-memory-server-global-4.4';
import { MongoMemoryServer } from 'mongodb-memory-server-global-4.4'; - MongoMemoryServer
import { MongoMemoryServer } from 'mongodb-memory-server-global-4.4'; // in CommonJSconst { MongoMemoryServer } = require('mongodb-memory-server-global-4.4'); - MongoMemoryServer.create()
const mongo = new MongoMemoryServer(); await mongo.start();
const mongo = await MongoMemoryServer.create();
Quickstart
import { MongoMemoryServer } from 'mongodb-memory-server-global-4.4';
import { MongoClient } from 'mongodb';
async function runTestDatabase() {
let mongoServer: MongoMemoryServer | null = null;
let client: MongoClient | null = null;
try {
// Start a new in-memory MongoDB server configured for 4.4
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
console.log(`MongoDB Memory Server started at: ${mongoUri}`);
// Connect a MongoDB client to the in-memory server
client = new MongoClient(mongoUri);
await client.connect();
console.log('MongoDB client connected successfully.');
// Perform some database operations
const db = client.db('testdb');
const collection = db.collection('documents');
await collection.insertOne({ name: 'Test Document', value: 1 });
const docs = await collection.find({}).toArray();
console.log('Inserted and found documents:', docs);
} catch (error) {
console.error('Error during test database operations:', error);
} finally {
// Ensure the client and server are always stopped
if (client) {
await client.close();
console.log('MongoDB client closed.');
}
if (mongoServer) {
await mongoServer.stop();
console.log('MongoDB Memory Server stopped.');
}
}
}
runTestDatabase();