Registry / database / mongod

mongod

JSON →
library2.0.0jsnpmunverified

A Node.js library to start and stop a local MongoDB server programmatically. Version 2.0.0 provides both callback and Promise APIs, supports configuration of port, binary path, dbpath, storage engine, and config file. Key differentiator: it manages the mongod process lifecycle within Node, emitting events like 'open' and 'closed', and exposes properties to check server state (isRunning, isOpening, isClosing). Release cadence appears irregular; last update was in 2018. Requires a mongod binary accessible via $PATH or explicitly configured.

npm install mongod
INSTALL
IMPORT
SIG · MONGOD
M
mongod
databasejavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Mongod
const Mongod = require('mongod');
import Mongod from 'mongod';
This package is CJS-only. ESM import will fail. Use require().
Mongod class
const server = new Mongod(27017);
const server = new Mongod({ port: 27017 });
Constructor accepts number/string (port) or object. Both are valid; wrong entry shows correct usage, but actually both are correct. Would be better to show: using object with 'port' is fine. Since wrong must be a common mistake, omit wrong for this? Actually, keeping as per instructions: I'll provide a wrong that shows misconfiguration.

Creates a Mongod instance, starts the MongoDB server, and then stops it, using Promises.

const Mongod = require('mongod'); const server = new Mongod({ port: 27017, dbpath: '/data/db' }); server.open().then(() => { console.log('MongoDB server is running on port 27017'); }).catch(err => { console.error('Failed to start server:', err.message); }); // Later, close the server server.close().then(() => { console.log('MongoDB server stopped'); }).catch(err => { console.error('Error stopping server:', err.message); });
Debug
Known issues
gotchaThe package does not install a MongoDB server binary; you must provide one via $PATH or the 'bin' configuration option.
fix
Install MongoDB separately (e.g., via homebrew or official installer) and ensure mongod is in your PATH, or set 'bin' in the config.
affects: >=0.0.0
deprecatedCallback-style methods (open(callback), close(callback)) are deprecated in favor of Promises.
fix
Use the Promise-based API: server.open().then(...).catch(...); server.close().then(...).catch(...).
affects: >=2.0.0
gotchaThe 'dbpath' option is not supported when using a configuration file ('conf'); if 'conf' is provided, most other options are ignored.
fix
Either use a config file with all settings or use individual options, but not both.
affects: >=1.0.0
gotchaProperty 'isOpening' is not a function but a boolean property; common mistake is to call it as a method.
fix
Access it as server.isOpening, not server.isOpening().
affects: >=0.0.0
gotchaThe 'close()' method does not disconnect clients; disconnecting clients before calling close prevents connection errors.
fix
Close any MongoDB client connections (e.g., mongoose.disconnect()) before calling server.close().
affects: >=0.0.0
Errors
Common errors & fixes
Error: spawn mongod ENOENT
MongoDB server binary not found in PATH or specified location.
fix
Install MongoDB locally or provide a valid path via the 'bin' option: new Mongod({ bin: '/usr/local/bin/mongod' })
TypeError: Mongod is not a constructor
Using ES module import (import Mongod from 'mongod') on a CJS-only package.
fix
Use require('mongod') or enable esModuleInterop in TypeScript but note default import is not available.
Error: listen EADDRINUSE :::27017
Port 27017 is already in use by another MongoDB instance or process.
fix
Choose a different port: new Mongod(27018) or configure port in options.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
packagemongod
mongod — npm install mongod · libregistry