MongoDB Memory Server Core
MongoDB Memory Server Core (v11.0.1) is a library designed to launch an in-memory MongoDB instance for integration and unit testing. This core package differs from the full `mongodb-memory-server` by *not* including the automatic download and management of MongoDB binaries, requiring users or other wrapper packages to provide the MongoDB executable. It enables developers to create isolated, disposable database environments programmatically, preventing test interference and ensuring a clean state for each run. The library is actively maintained with frequent updates, including version 11.0.0 and subsequent patches released in December 2025, and currently supports Node.js versions 20.19.0 and higher. Its primary use case is providing a robust, isolated MongoDB server for various ODM and client libraries within a testing suite.
Common errors
-
Error: A MongoDB binary could not be found.
cause The `mongodb-memory-server-core` package does not download the MongoDB binary by default, and it was not found in expected locations or configured paths.fixUse the full `mongodb-memory-server` package, which includes binary autodownload. Alternatively, set the `MONGOMS_DOWNLOAD_DIR` environment variable to a path where binaries should be downloaded, or `MONGOMS_SYSTEM_BINARY` to the path of an existing MongoDB executable. -
MongoServerError: No primary found for replica set rs0
cause This typically occurs when a `MongoMemoryReplSet` is used, but the replica set hasn't fully initialized or achieved a primary state before the client attempts to connect or perform operations.fixEnsure you `await` the `start()` method of `MongoMemoryReplSet` and consider adding a small delay or a retry mechanism for client connections in your tests to account for replica set election time. -
ERR_REQUIRE_ESM: require() of ES Module ...mongodb-memory-server-core/index.js from ... not supported.
cause The package `mongodb-memory-server-core` is an ECMAScript Module (ESM) and cannot be imported using `require()` in newer Node.js environments (v20.19.0+) that strictly enforce ESM.fixUpdate your import statements to use ESM `import` syntax: `import { MongoMemoryServer } from 'mongodb-memory-server-core';`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
Error: Node.js v18.x.x is not supported! Please use Node.js v20.19.0 or newer.
cause Attempting to run `mongodb-memory-server-core` v11+ with an unsupported Node.js version, as indicated by the breaking change.fixUpgrade your Node.js runtime to version 20.19.0 or a newer compatible version. Check the `engines.node` field in the `package.json` for current requirements.
Warnings
- breaking Version 11.0.0 removed support for Node.js versions below 20.19.0. Projects using older Node.js runtimes must upgrade their environment or stick to `mongodb-memory-server-core` v10.x.x.
- breaking The default MongoDB binary version used by `mongodb-memory-server-core` (when not explicitly configured) was updated to 8.2.x in v11.0.0. This might cause compatibility issues if your application or driver expects an older MongoDB version.
- breaking Support for MongoDB binary versions below 4.2.0 was removed in version 11.0.0.
- breaking The `tsconfig` target was upgraded to `es2023` in version 11.0.0, which could cause compilation issues in projects targeting older ECMAScript versions or using outdated TypeScript configurations.
- gotcha This is the 'core' package (`mongodb-memory-server-core`) and it *does not* automatically download the MongoDB binary. Users are responsible for providing the binary, either manually or via another package like `mongodb-memory-server` (the meta-package).
Install
-
npm install mongodb-memory-server-core -
yarn add mongodb-memory-server-core -
pnpm add mongodb-memory-server-core
Imports
- MongoMemoryServer
const { MongoMemoryServer } = require('mongodb-memory-server-core');import { MongoMemoryServer } from 'mongodb-memory-server-core'; - MongoMemoryReplSet
import MongoMemoryReplSet from 'mongodb-memory-server-core';
import { MongoMemoryReplSet } from 'mongodb-memory-server-core'; - StartOptions
import type { StartOptions } from 'mongodb-memory-server-core';
Quickstart
import { MongoMemoryServer } from 'mongodb-memory-server-core';
import { MongoClient } from 'mongodb';
async function runInMemoryMongo() {
let mongod: MongoMemoryServer | undefined;
let client: MongoClient | undefined;
try {
mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
console.log(`MongoDB Memory Server started at: ${uri}`);
client = new MongoClient(uri);
await client.connect();
console.log('Connected to MongoDB Memory Server.');
const db = client.db('testdb');
const collection = db.collection('documents');
await collection.insertOne({ name: 'Test Document', value: 123 });
const doc = await collection.findOne({ name: 'Test Document' });
console.log('Found document:', doc);
} catch (error) {
console.error('Error during in-memory MongoDB operation:', error);
} finally {
if (client) {
await client.close();
console.log('MongoDB client closed.');
}
if (mongod) {
await mongod.stop();
console.log('MongoDB Memory Server stopped.');
}
}
}
runInMemoryMongo();