Registry / devops / mongo-healthcheck

mongo-healthcheck

JSON →
library2.0.1jsnpmunverified

A lightweight healthcheck function for Mongoose connections to MongoDB. Version 2.0.1 is the current stable release. It provides a simple synchronous check to determine if a Mongoose connection is ready, returning true if connected, false if not connected, or throwing an error on invalid input. It differs from more comprehensive healthcheck libraries by focusing solely on Mongoose and being minimal in dependencies. Release cadence is low; package has not been updated since 2019.

npm install mongo-healthcheck
INSTALL
IMPORT
SIG · MONGO-HEALTHCHECK
M
mongo-healthcheck
devopsjavascriptv2.0.1
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.

default
const mongoHealthcheck = require('mongo-healthcheck');
import mongoHealthcheck from 'mongo-healthcheck';
Package uses CommonJS and does not provide ESM exports.
default
import mongoHealthcheck from 'mongo-healthcheck';
const mongoHealthcheck = require('mongo-healthcheck').default;
ESM import works but only if using Node.js with ESM support; the package has no default export in CJS sense.
default
const mh = require('mongo-healthcheck');
const { mongoHealthcheck } = require('mongo-healthcheck');
The package exports a single function as module.exports, not a named export.

Shows how to use mongo-healthcheck to check Mongoose connection state and return health status in an Express endpoint.

const mongoHealthcheck = require('mongo-healthcheck'); const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true }); // In an HTTP handler: app.get('/health', (req, res) => { let isError = false; let mongoResult; try { mongoResult = mongoHealthcheck(mongoose); } catch (err) { isError = true; mongoResult = err.message; } res.status(isError ? 500 : 200).json({ mongo: mongoResult }); });
Debug
Known issues
gotchaPassing a mongoose instance that is not yet connected may return false instead of an error.
fix
Ensure mongoose.connect() has completed before calling healthcheck.
affects: >=1.0.0
gotchaThe function does not throw an error for invalid input unless the argument is null or undefined.
fix
Always pass a valid mongoose instance.
affects: >=1.0.0
deprecatedOld mongoose connection patterns (pre-5.x) may not be handled correctly.
fix
Use mongoose version 5.x or newer with unified topology.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: mongoHealthcheck is not a function
Attempting to destructure the import incorrectly.
fix
Use const mongoHealthcheck = require('mongo-healthcheck');
mongoHealthcheck is not a function
Using ESM import incorrectly or importing default as named.
fix
Use const mongoHealthcheck = require('mongo-healthcheck'); or import mongoHealthcheck from 'mongo-healthcheck';
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
mongooserequiredRequired to check the connection state of a Mongoose instance.
Agent activity
2 hits · last 30 days
node
2
Resources
mongo-healthcheck — npm install mongo-healthcheck · libregistry