{"id":11358,"library":"mongodb-memory-server-global","title":"MongoDB Memory Server Global","description":"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.","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","lang":"bash","label":"npm"},{"cmd":"yarn add mongodb-memory-server-global","lang":"bash","label":"yarn"},{"cmd":"pnpm add mongodb-memory-server-global","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This package handles binary downloads; actual programmatic server control is provided by 'mongodb-memory-server'.","package":"mongodb-memory-server","optional":false},{"reason":"Used by Mongoose or the MongoDB Node.js driver to connect to the server.","package":"mongodb","optional":true},{"reason":"A popular ODM that often utilizes mongodb-memory-server for testing.","package":"mongoose","optional":true}],"imports":[{"note":"The 'mongodb-memory-server-global' package primarily manages binary downloads. For programmatic control (starting/stopping the server), import `MongoMemoryServer` from the core `mongodb-memory-server` package.","wrong":"import { MongoMemoryServer } from 'mongodb-memory-server-global'","symbol":"MongoMemoryServer","correct":"import { MongoMemoryServer } from 'mongodb-memory-server'"},{"note":"Similar to `MongoMemoryServer`, replication set functionality is exported by `mongodb-memory-server`. Use named imports for ESM environments, and `require` for CommonJS.","wrong":"const { MongoMemoryReplSet } = require('mongodb-memory-server-global')","symbol":"MongoMemoryReplSet","correct":"import { MongoMemoryReplSet } from 'mongodb-memory-server'"},{"note":"For a simpler API to just start the server without explicit class instantiation, `start` is available as a named export from `mongodb-memory-server`. Avoid star imports unless necessary for specific legacy patterns.","wrong":"import * as MMS from 'mongodb-memory-server'","symbol":"start","correct":"import { start } from 'mongodb-memory-server'"}],"quickstart":{"code":"import { MongoMemoryServer } from 'mongodb-memory-server';\nimport mongoose from 'mongoose';\n\nasync function runInMemoryDbTest() {\n  // mongodb-memory-server-global ensures binaries are available.\n  // MongoMemoryServer handles the in-memory instance.\n  const mongod = await MongoMemoryServer.create();\n  const uri = mongod.getUri();\n\n  console.log(`MongoDB Memory Server started at: ${uri}`);\n\n  try {\n    await mongoose.connect(uri);\n    console.log('Mongoose connected to in-memory MongoDB');\n\n    const UserSchema = new mongoose.Schema({ name: String, email: String });\n    const User = mongoose.model('User', UserSchema);\n\n    const newUser = await User.create({ name: 'Test User', email: 'test@example.com' });\n    console.log('User created:', newUser);\n\n    const foundUser = await User.findOne({ name: 'Test User' });\n    console.log('User found:', foundUser);\n\n  } catch (error) {\n    console.error('Error during test:', error);\n  } finally {\n    await mongoose.disconnect();\n    console.log('Mongoose disconnected.');\n    await mongod.stop();\n    console.log('MongoDB Memory Server stopped.');\n  }\n}\n\nrunInMemoryDbTest();\n","lang":"typescript","description":"Demonstrates starting an in-memory MongoDB instance, connecting with Mongoose, performing basic CRUD operations, and then cleaning up the connection and server. This setup implicitly relies on `mongodb-memory-server-global` having pre-downloaded the necessary binaries."},"warnings":[{"fix":"If specific MongoDB versions are required, set the `mongodb-version` configuration option (e.g., in `package.json` or via environment variables) to pin to an older version, for example `MONGOMS_VERSION=7.0.0`.","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Ensure your project's MongoDB version requirements are 4.2.0 or higher. Update your `mongodb-version` configuration if you were using an unsupported version.","message":"Support for MongoDB versions below 4.2.0 has been removed. Trying to specify older versions will result in download or startup failures.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Upgrade your Node.js environment to version 20.19.0 or higher. Use a Node.js version manager like `nvm` to manage multiple Node.js versions.","message":"The minimum supported Node.js version is now 20.19.0. Running on older Node.js environments will lead to errors.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Update your project's `tsconfig.json` to be compatible with `es2023` or ensure your build process correctly transpiles newer ECMAScript features.","message":"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.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Migrate your tests and applications to use a newer MongoDB version (4.2.0 or higher) and the main `mongodb-memory-server-global` package.","message":"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.","severity":"deprecated","affected_versions":">=11.0.0"},{"fix":"Ensure stable internet connectivity. If behind a proxy, configure `HTTP_PROXY` or `HTTPS_PROXY` environment variables. For specific issues, setting `MONGOMS_USE_HTTP=1` might resolve download problems caused by `http` module limitations in some environments.","message":"Binary download failures can occur due to network issues or incorrect proxy configurations. Recent versions include fixes for `USE_HTTP` requests.","severity":"gotcha","affected_versions":">=10.4.3, >=11.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Check 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.","cause":"The `mongodb-memory-server-global` package failed to download the required MongoDB binary during installation, or the specified version is unsupported/unavailable.","error":"Error: MongoDB binary for version X.Y.Z not found."},{"fix":"Change your import statement from `import { MongoMemoryServer } from 'mongodb-memory-server-global'` to `import { MongoMemoryServer } from 'mongodb-memory-server'`.","cause":"Attempting to use `MongoMemoryServer` directly from `mongodb-memory-server-global` instead of `mongodb-memory-server`.","error":"TypeError: MongoMemoryServer.create is not a function"},{"fix":"Upgrade 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.","cause":"Running `mongodb-memory-server` with an unsupported Node.js version.","error":"Node.js vX.Y.Z is not supported. Please use Node.js >= 20.19.0"}],"ecosystem":"npm"}