{"id":11356,"library":"mongodb-memory-server-core","title":"MongoDB Memory Server Core","description":"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.","status":"active","version":"11.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/typegoose/mongodb-memory-server","tags":["javascript","mongodb","mongoose","mock","stub","mockgoose","mongodb-prebuilt","mongomem","typescript"],"install":[{"cmd":"npm install mongodb-memory-server-core","lang":"bash","label":"npm"},{"cmd":"yarn add mongodb-memory-server-core","lang":"bash","label":"yarn"},{"cmd":"pnpm add mongodb-memory-server-core","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily targets ESM environments since Node.js 20.19.0 and tsconfig es2023 in v11.","wrong":"const { MongoMemoryServer } = require('mongodb-memory-server-core');","symbol":"MongoMemoryServer","correct":"import { MongoMemoryServer } from 'mongodb-memory-server-core';"},{"note":"Used for testing applications that require a MongoDB replica set configuration, imported as a named export.","wrong":"import MongoMemoryReplSet from 'mongodb-memory-server-core';","symbol":"MongoMemoryReplSet","correct":"import { MongoMemoryReplSet } from 'mongodb-memory-server-core';"},{"note":"Type import for configuration options when starting the server or replica set.","symbol":"StartOptions","correct":"import type { StartOptions } from 'mongodb-memory-server-core';"}],"quickstart":{"code":"import { MongoMemoryServer } from 'mongodb-memory-server-core';\nimport { MongoClient } from 'mongodb';\n\nasync function runInMemoryMongo() {\n  let mongod: MongoMemoryServer | undefined;\n  let client: MongoClient | undefined;\n\n  try {\n    mongod = await MongoMemoryServer.create();\n    const uri = mongod.getUri();\n    console.log(`MongoDB Memory Server started at: ${uri}`);\n\n    client = new MongoClient(uri);\n    await client.connect();\n    console.log('Connected to MongoDB Memory Server.');\n\n    const db = client.db('testdb');\n    const collection = db.collection('documents');\n\n    await collection.insertOne({ name: 'Test Document', value: 123 });\n    const doc = await collection.findOne({ name: 'Test Document' });\n    console.log('Found document:', doc);\n\n  } catch (error) {\n    console.error('Error during in-memory MongoDB operation:', error);\n  } finally {\n    if (client) {\n      await client.close();\n      console.log('MongoDB client closed.');\n    }\n    if (mongod) {\n      await mongod.stop();\n      console.log('MongoDB Memory Server stopped.');\n    }\n  }\n}\n\nrunInMemoryMongo();\n","lang":"typescript","description":"Demonstrates starting an in-memory MongoDB server, connecting with the official driver, performing a basic insert and find operation, and properly shutting down the client and server."},"warnings":[{"fix":"Upgrade your Node.js environment to version 20.19.0 or newer. Alternatively, pin `mongodb-memory-server-core` to a version prior to 11.0.0.","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Explicitly define the desired MongoDB version using the `version` option in the `create` or `start` method, e.g., `MongoMemoryServer.create({ instance: { version: '6.0.0' } })`.","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Ensure your tests use MongoDB versions 4.2.0 or newer. If older versions are strictly required, you must use `mongodb-memory-server-core` v10.x.x or earlier.","message":"Support for MongoDB binary versions below 4.2.0 was removed in version 11.0.0.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Update your project's TypeScript configuration to target `es2023` or newer, and ensure your TypeScript compiler is up-to-date (typically TypeScript 5.x or newer).","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"If you expect automatic binary downloads, use the `mongodb-memory-server` package directly. If using `mongodb-memory-server-core`, ensure the `MONGOMS_DOWNLOAD_DIR` or `MONGOMS_SYSTEM_BINARY` environment variables are correctly configured, or manage the binary installation externally.","message":"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).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use 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.","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.","error":"Error: A MongoDB binary could not be found."},{"fix":"Ensure 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.","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.","error":"MongoServerError: No primary found for replica set rs0"},{"fix":"Update 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`).","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.","error":"ERR_REQUIRE_ESM: require() of ES Module ...mongodb-memory-server-core/index.js from ... not supported."},{"fix":"Upgrade 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.","cause":"Attempting to run `mongodb-memory-server-core` v11+ with an unsupported Node.js version, as indicated by the breaking change.","error":"Error: Node.js v18.x.x is not supported! Please use Node.js v20.19.0 or newer."}],"ecosystem":"npm"}