Registry /
database / monk-middleware-cast-ids
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
default
✓ import castIds from 'monk-middleware-cast-ids'
✗ const castIds = require('monk-middleware-cast-ids')
Package exports a default function. CommonJS require might work but ESM-style import is recommended.
castIds (in CommonJS)
✓ const castIds = require('monk-middleware-cast-ids').default
✗ const castIds = require('monk-middleware-cast-ids')
If using require in a CommonJS environment, note that the module might use ESM, requiring .default access. Check your bundler.
TypeScript types
✓ import castIds from 'monk-middleware-cast-ids'
✗ import { castIds } from 'monk-middleware-cast-ids'
No named export 'castIds', so braces will cause runtime error.
Demonstrates how to add the cast-ids middleware to a Monk collection, enabling automatic string-to-ObjectId conversion.
import monk from 'monk';
import castIds from 'monk-middleware-cast-ids';
const db = monk('localhost/myapp', {
useUnifiedTopology: true,
});
const collection = db.get('users');
collection.addMiddleware(castIds);
// Now string IDs are automatically cast to ObjectId
const result = await collection.findOne({ _id: '507f1f77bcf86cd799439011' });
console.log(result);
Errors
Common errors & fixes
TypeError: castIds is not a function
Using CommonJS require incorrectly: const castIds = require('monk-middleware-cast-ids') returns an object with a default property.
fixUse const castIds = require('monk-middleware-cast-ids').default; Cannot find module 'monk-middleware-cast-ids'
Package not installed; misspelled name; or used in Monk v7 where middleware is not supported.
fixInstall package: npm install monk-middleware-cast-ids; check spelling; confirm Monk version <7.
CastError: Cast to ObjectId failed for value "invalid" at path "_id"
The middleware attempted to cast a string that is not a valid 24-character hex ObjectId.
fixEnsure IDs are valid 24-character hex strings (e.g., '507f1f77bcf86cd799439011') before passing them.
Error: Cannot add middleware after first operation
Middleware must be added before any database operations are performed on the collection.
fixAdd middleware immediately after getting the collection, before any find/insert/etc.
Audit
Dependencies
monkrequiredThis is a middleware for monk and requires it to be installed as a peer dependency.