Registry / database / objectid

objectid

JSON →
library3.2.1jsnpmunverified

MongoDB ObjectID utilities library, version 3.2.1. Provides creation, validation, casting, and comparison of MongoDB ObjectIDs. Lightweight and focused on compatibility with the native MongoDB driver (npm `mongodb`). Supports both Node.js and browser environments with distinct major versions. Static methods like `isValid`, `tryParse`, and `equals` complement instance methods. Released under MIT license.

npm install objectid
INSTALL
IMPORT
SIG · OBJECTID
O
objectid
databasejavascriptv3.2.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 export (factory function)
import objectid from 'objectid'; const id = objectid();
const objectid = require('objectid'); // may be mistaken for default import but works in CJS
ESM default import; also works with CommonJS require().
objectid.isValid
import objectid from 'objectid'; objectid.isValid('...');
const { isValid } = require('objectid'); // isValid is not a named export; use objectid.isValid
isValid is a static method on the default export.
objectid.equals
import objectid from 'objectid'; objectid.equals(a, b);
import { equals } from 'objectid'; // no named export
equals is static method on default export.

Shows creation, validation, casting, and equality checking of MongoDB ObjectIDs.

import objectid from 'objectid'; // Create a new ObjectId const id = objectid(); console.log(id.toString()); // e.g., '507f191e810c19729de860ea' // Validate a hex string console.log(objectid.isValid('507f191e810c19729de860ea')); // true console.log(objectid.isValid('invalid')); // false // Cast from string or ObjectID const id2 = objectid('507f191e810c19729de860ea'); console.log(id.equals(id2)); // true (if based on same value) // Static equality check console.log(objectid.equals(id, id2)); // true
Debug
Known issues
breakingVersion 3.x changed to focus on npm mongodb driver compatibility; 1.x is for browser.
fix
Use 1.x for browser environments, or ensure compatibility with mongodb driver.
affects: >=3.0.0
gotchaobjectid.isValid returns true for any object with a .toString method returning a valid hex string, not just ObjectID instances.
fix
If you need strict ObjectID instance check, use instanceof or driver-specific method.
affects: >=1.0.0
gotchaobjectid() throws if argument is not a valid ObjectId; always validate first if uncertain.
fix
Use objectid.isValid() before calling objectid(arg).
affects: >=1.0.0
deprecatedNo documented deprecations in current version; check release notes for future changes.
affects: >=3.2.1
Errors
Common errors & fixes
TypeError: objectid is not a function
Importing the default export incorrectly in ESM (e.g., using named import).
fix
Use import objectid from 'objectid' instead of import { objectid } from 'objectid'.
Error: invalid objectid
Calling objectid() with an invalid string or non-convertible value.
fix
First validate with objectid.isValid() before passing to objectid().
ReferenceError: require is not defined in ES module scope
Using require() in an ESM context without using createRequire.
fix
Use import objectid from 'objectid' instead of const objectid = require('objectid').
Upgrade
Version history
3.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
objectid — npm install objectid · libregistry