MongoDB Memory Server is a robust Node.js library designed to provide a real, in-memory MongoDB server instance for testing purposes, eliminating the need for an external MongoDB installation. It automatically downloads the appropriate MongoDB binary to a local cache directory (`./node_modules/.cache`) upon package installation or first run, ensuring that tests are isolated and reproducible. The current stable version is 11.0.1. The package maintains an active release cadence, frequently pushing patches and minor updates, with major versions typically aligning with significant MongoDB binary version changes or Node.js LTS updates. It's particularly useful for integration tests, allowing developers to connect their preferred ODM (like Mongoose or Typegoose) or client library to a fresh, isolated MongoDB instance for each test suite, thereby preventing state leakage between tests and speeding up CI/CD pipelines.
npm install mongodb-memory-serverVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize, connect to, perform a basic operation, and cleanly shut down a MongoDB Memory Server instance using the official `mongodb` driver.
If you require an older MongoDB version for compatibility, you must explicitly configure it using the `binary.version` option in the `MongoMemoryServer` constructor or via environment variables (e.g., `MONGODB_VERSION=7.0.x`).
Upgrade your Node.js environment to version 20.19.0 or higher. Check your `engines` field in `package.json` and CI/CD configurations.
Ensure your tests are compatible with MongoDB 4.2.0 or newer. If testing legacy MongoDB features is necessary, you will need to stick to `mongodb-memory-server` v10.x or earlier.
Update your project's `tsconfig.json` to target `es2023` or higher, or ensure your TypeScript compiler (`tsc`) version is up-to-date and compatible with `es2023` features.
Configure proxy settings via environment variables (e.g., `HTTP_PROXY`, `HTTPS_PROXY`), increase download timeouts (`DOWNLOAD_TIMEOUT`), or pre-fetch the binary manually if running in an air-gapped environment. Ensure firewalls are not blocking the download URLs.
Ensure you `await mongoServer.start()` before attempting to connect your client or ODM. Check the console for any errors during the `start()` process indicating binary download or startup issues.
If your application requires replica set functionality (e.g., transactions, change streams), you must use `MongoMemoryReplSet` instead of `MongoMemoryServer`. Initialize with `new MongoMemoryReplSet()` and configure appropriate replica set options.
Verify your network connectivity and proxy settings. Check the `--debug` flag or `DEBUG=mongodb-memory-server:*` environment variable for detailed download logs. You might also need to clear the cache (`npm cache clean --force` or manually delete `.cache` directory) and reinstall.
No dependency data recorded yet.