MongoDB Memory Server Global
MongoDB Memory Server Global (mongodb-memory-server-global) provides a convenient, pre-configured way to set up and manage an in-memory MongoDB instance for testing and development. As of version 11.0.1, it automates the download and caching of the latest MongoDB server binaries (defaulting to 8.2.x since v11) to a user's local cache directory (`~/.cache/mongodb-binaries`) upon installation. This package acts primarily as an installation wrapper, simplifying the environment setup for its counterpart, `mongodb-memory-server`, which offers the actual programmatic API to start, stop, and manage the in-memory database instances. It aims for a stable release cadence, with patch and minor updates released frequently, and major versions introducing significant breaking changes and updated MongoDB defaults. Key differentiators include its ease of setup for CI/CD environments and its ability to quickly spin up isolated database instances without requiring a pre-installed MongoDB server.
Common errors
-
Error: MongoDB binary for version X.Y.Z not found.
cause The `mongodb-memory-server-global` package failed to download the required MongoDB binary during installation, or the specified version is unsupported/unavailable.fixCheck your network connection and proxy settings. Ensure the requested MongoDB version is supported by `mongodb-memory-server` (e.g., >=4.2.0 for v11+). Try deleting the `~/.cache/mongodb-binaries` directory and reinstalling, or manually specify a supported version with the `MONGOMS_VERSION` environment variable. -
TypeError: MongoMemoryServer.create is not a function
cause Attempting to use `MongoMemoryServer` directly from `mongodb-memory-server-global` instead of `mongodb-memory-server`.fixChange your import statement from `import { MongoMemoryServer } from 'mongodb-memory-server-global'` to `import { MongoMemoryServer } from 'mongodb-memory-server'`. -
Node.js vX.Y.Z is not supported. Please use Node.js >= 20.19.0
cause Running `mongodb-memory-server` with an unsupported Node.js version.fixUpgrade your Node.js environment to version 20.19.0 or newer. Consider using a Node Version Manager (NVM) to manage different Node.js versions for various projects.
Warnings
- breaking The default MongoDB binary version used by `mongodb-memory-server` has been updated to 8.2.x. This might affect tests expecting older MongoDB behavior or specific features.
- breaking Support for MongoDB versions below 4.2.0 has been removed. Trying to specify older versions will result in download or startup failures.
- breaking The minimum supported Node.js version is now 20.19.0. Running on older Node.js environments will lead to errors.
- breaking The TypeScript target in the project's `tsconfig` has been upgraded to `es2023`. This could potentially cause issues in projects with older TypeScript configurations or environments expecting a different target.
- deprecated The specific package `mongodb-memory-server-global-4.0` has been removed as MongoDB 4.0 is no longer supported by the latest MongoDB driver.
- gotcha Binary download failures can occur due to network issues or incorrect proxy configurations. Recent versions include fixes for `USE_HTTP` requests.
Install
-
npm install mongodb-memory-server-global -
yarn add mongodb-memory-server-global -
pnpm add mongodb-memory-server-global
Imports
- MongoMemoryServer
import { MongoMemoryServer } from 'mongodb-memory-server-global'import { MongoMemoryServer } from 'mongodb-memory-server' - MongoMemoryReplSet
const { MongoMemoryReplSet } = require('mongodb-memory-server-global')import { MongoMemoryReplSet } from 'mongodb-memory-server' - start
import * as MMS from 'mongodb-memory-server'
import { start } from 'mongodb-memory-server'
Quickstart
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
async function runInMemoryDbTest() {
// mongodb-memory-server-global ensures binaries are available.
// MongoMemoryServer handles the in-memory instance.
const mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
console.log(`MongoDB Memory Server started at: ${uri}`);
try {
await mongoose.connect(uri);
console.log('Mongoose connected to in-memory MongoDB');
const UserSchema = new mongoose.Schema({ name: String, email: String });
const User = mongoose.model('User', UserSchema);
const newUser = await User.create({ name: 'Test User', email: 'test@example.com' });
console.log('User created:', newUser);
const foundUser = await User.findOne({ name: 'Test User' });
console.log('User found:', foundUser);
} catch (error) {
console.error('Error during test:', error);
} finally {
await mongoose.disconnect();
console.log('Mongoose disconnected.');
await mongod.stop();
console.log('MongoDB Memory Server stopped.');
}
}
runInMemoryDbTest();