{"id":16127,"library":"mongodb-memory-server-global-4.4","title":"MongoDB Memory Server Global 4.4","description":"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.","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-global-4.4","lang":"bash","label":"npm"},{"cmd":"yarn add mongodb-memory-server-global-4.4","lang":"bash","label":"yarn"},{"cmd":"pnpm add mongodb-memory-server-global-4.4","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"MongoMemoryServer is a named export, not a default export. This package re-exports the main class pre-configured for MongoDB 4.4.","wrong":"import MongoMemoryServer from 'mongodb-memory-server-global-4.4';","symbol":"MongoMemoryServer","correct":"import { MongoMemoryServer } from 'mongodb-memory-server-global-4.4';"},{"note":"While CommonJS `require` is supported, ESM `import` syntax is recommended for newer Node.js versions (>=20.19.0) and TypeScript projects.","wrong":"import { MongoMemoryServer } from 'mongodb-memory-server-global-4.4'; // in CommonJS","symbol":"MongoMemoryServer","correct":"const { MongoMemoryServer } = require('mongodb-memory-server-global-4.4');"},{"note":"The static `create()` method is the recommended asynchronous factory for initializing and starting the server, handling binary download and setup internally.","wrong":"const mongo = new MongoMemoryServer();\nawait mongo.start();","symbol":"MongoMemoryServer.create()","correct":"const mongo = await MongoMemoryServer.create();"}],"quickstart":{"code":"import { MongoMemoryServer } from 'mongodb-memory-server-global-4.4';\nimport { MongoClient } from 'mongodb';\n\nasync function runTestDatabase() {\n  let mongoServer: MongoMemoryServer | null = null;\n  let client: MongoClient | null = null;\n  try {\n    // Start a new in-memory MongoDB server configured for 4.4\n    mongoServer = await MongoMemoryServer.create();\n    const mongoUri = mongoServer.getUri();\n    console.log(`MongoDB Memory Server started at: ${mongoUri}`);\n\n    // Connect a MongoDB client to the in-memory server\n    client = new MongoClient(mongoUri);\n    await client.connect();\n    console.log('MongoDB client connected successfully.');\n\n    // Perform some database operations\n    const db = client.db('testdb');\n    const collection = db.collection('documents');\n    await collection.insertOne({ name: 'Test Document', value: 1 });\n    const docs = await collection.find({}).toArray();\n    console.log('Inserted and found documents:', docs);\n\n  } catch (error) {\n    console.error('Error during test database operations:', error);\n  } finally {\n    // Ensure the client and server are always stopped\n    if (client) {\n      await client.close();\n      console.log('MongoDB client closed.');\n    }\n    if (mongoServer) {\n      await mongoServer.stop();\n      console.log('MongoDB Memory Server stopped.');\n    }\n  }\n}\n\nrunTestDatabase();","lang":"typescript","description":"Demonstrates how to start an in-memory MongoDB 4.4 server, connect a native MongoDB client, perform a basic database operation, and then properly shut down both the client and the server."},"warnings":[{"fix":"Upgrade your Node.js runtime to version 20.19.0 or newer. Check your `package.json` engines field and CI/CD environments.","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Ensure your project's `tsconfig.json` is compatible with `es2023` or newer. You might need to update your TypeScript compiler version or adjust your project's `target` setting.","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"If you need an older MongoDB version for testing, you must use an older version of `mongodb-memory-server` or one of its global packages that supports it. For version 4.4, this package is suitable, but do not try to configure it to <4.2.","message":"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.","severity":"gotcha","affected_versions":">=11.0.0"},{"fix":"Migrate your tests to use `mongodb-memory-server-global-4.4` or a newer `mongodb-memory-server-global-` package to maintain compatibility with modern MongoDB drivers and Node.js versions.","message":"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.","severity":"deprecated","affected_versions":">=11.0.0 (for users of global-4.0)"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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`.","error":"Error: MongoDB binary for version X.Y.Z is not supported on this platform."},{"fix":"Verify 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.","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.","error":"Error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:xxxxx"},{"fix":"Ensure 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.","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.","error":"TypeError: Cannot read properties of undefined (reading 'connect')"}],"ecosystem":"npm"}