MongoDB Runner is a utility for programmatically or via CLI spinning up and managing MongoDB server instances and clusters specifically for testing purposes. It simplifies the process of testing code against various MongoDB topologies like standalone, replica sets, or sharded clusters, without requiring manual MongoDB installation or management. The current stable version is 6.7.7, and based on its development activity, it appears to have a regular release cadence, often aligning with new MongoDB server releases or bug fixes. A key differentiator is its ability to download and manage multiple MongoDB server versions locally, including enterprise versions, and to integrate with Docker. It offers both a convenient command-line interface via `npx` and a robust programmatic API for integration into test suites, abstracting away the complexities of `mongod` and `mongos` commands.
npm install mongodb-runnerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically start a standalone MongoDB instance, log its connection string, and then gracefully shut it down using the `mongodb-runner` API.
Review the official `mongodb-runner` documentation for version 5 and above to understand the new API and CLI commands. Update import paths, method calls, and configuration options accordingly.
Always use `npx mongodb-runner start -t replset -- --setParameter allowDiskUseByDefault=true` where `--setParameter` is for the MongoDB server, and `--port` is for `mongodb-runner`.
To manage download locations, use the `--downloadDir` option. For CI/CD, consider pre-downloading binaries or caching the `--downloadDir` across builds to reduce setup time.
Always ensure `cluster.close()` is called in programmatic usage, ideally within a `finally` block. For CLI usage, use `npx mongodb-runner stop --all` or `npx mongodb-runner stop --id <cluster_id>`.
Either stop the existing process, or specify a different port for `mongodb-runner` using `npx mongodb-runner start -- --port <new_port>` for CLI, or `await MongoCluster.start({ port: <new_port> })` for programmatic use.Ensure your `package.json` has `"type": "module"` for ESM, or use `require` syntax if you must stick to CommonJS (though ESM is the preferred API for `mongodb-runner` v5+). Alternatively, run your script with `node --experimental-modules your-script.js` if Node.js version is older and doesn't natively support ESM.
Ensure you are using `import { MongoCluster } from 'mongodb-runner';` and calling `await MongoCluster.start(...)`. If you are on an older version, consult the documentation for that specific version.No dependency data recorded yet.