Registry / database / bson-objectid

bson-objectid

JSON →
library2.0.4jsnpmunverified

Lightweight standalone ObjectID constructor compatible with MongoDB BSON ObjectID API. v2.0.4 is stable; releases are infrequent. Features zero-dependency creation and parsing of ObjectIDs without mongodb/bson driver — ideal for browser or server-side apps that need ObjectID generation/comparison but not full MongoDB client. Provides full API parity with official BSON ObjectID including createFromTime, createFromHexString, isValid, getTimestamp, and immutable instances. Ships TypeScript types. Supports ESM and CJS.

npm install bson-objectid
INSTALL
IMPORT
SIG · BSON-OBJECTID
B
bson-objectid
databasejavascriptv2.0.4
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.

ObjectID
✓ import ObjectID from 'bson-objectid'
✗ const ObjectID = require('bson-objectid')
Default ESM import works in TypeScript and modern bundlers.
ObjectID (type)
✓ import type { ObjectID } from 'bson-objectid'
For TypeScript type-only imports.
ObjectID (CJS require)
✓ const ObjectID = require('bson-objectid')
CJS still works, but ESM is preferred.
ObjectID (named import)
✓ import { ObjectID } from 'bson-objectid'
✗ import { ObjectID } from 'bson-objectid'
Named import is also supported and commonly used.

Demonstrates ObjectID creation from different inputs, validation, equality, and timestamp extraction.

import ObjectID from 'bson-objectid'; // Generate a new ObjectID const id = ObjectID(); console.log(id.str); // 12-byte string console.log(id.toHexString()); // 24-character hex string // Create from hex string const fromHex = ObjectID('54495ad94c934721ede76d90'); console.log(fromHex.getTimestamp()); // Date object // Validate console.log(ObjectID.isValid('54495ad94c934721ede76d90')); // true console.log(ObjectID.isValid('invalid')); // false // Equality const id2 = ObjectID('54495ad94c934721ede76d90'); console.log(fromHex.equals(id2)); // true // Create from timestamp const timeBased = ObjectID.createFromTime(1414093117); console.log(timeBased.toHexString()); // hex with zeroed rest
Debug
Known issues
gotchaObjectID() without 'new' works as a constructor but returns an immutable instance; some may expect to use 'new ObjectID()'.
fix
Either use ObjectID() without new or new ObjectID(); both produce same result.
affects: >=1.0.0
gotchaObjectID.isValid accepts both ObjectID instances and 24-character hex strings; does NOT accept integer IDs or other types.
fix
Ensure argument is either an ObjectID instance or a 24-char hex string.
affects: >=1.0.0
gotchaObjectID.createFromHexString and ObjectID.createFromTime cannot be called as static on an instance; must be called on the ObjectID constructor itself.
fix
Use ObjectID.createFromHexString(hex) not id.createFromHexString(hex).
affects: >=1.0.0
deprecatedThe 'id' property returns a 12-byte string; prefer 'toHexString()' for hex representation.
fix
Use .toHexString() instead of .id for hex string.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: ObjectID is not a constructor
Attempting to use 'new ObjectID()' with a default import that is not a constructor (though it works).
fix
Use ObjectID() without new, or ensure import is correct.
Cannot read properties of undefined (reading 'str')
Called ObjectID() without arguments but forgot to call it (e.g., ObjectID instead of ObjectID()).
fix
Call ObjectID() with or without arguments.
Upgrade
Version history
2.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
8
OpenAI (training)
1
Resources
bson-objectid — npm install bson-objectid · libregistry