{"id":42474,"library":"bson-objectid","title":"bson-objectid","description":"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.","status":"active","version":"2.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/williamkapke/bson-objectid","tags":["javascript","ObjectID","mongo","mongodb","bson","createFromHexString","hex","typescript"],"install":[{"cmd":"npm install bson-objectid","lang":"bash","label":"npm"},{"cmd":"yarn add bson-objectid","lang":"bash","label":"yarn"},{"cmd":"pnpm add bson-objectid","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default ESM import works in TypeScript and modern bundlers.","wrong":"const ObjectID = require('bson-objectid')","symbol":"ObjectID","correct":"import ObjectID from 'bson-objectid'"},{"note":"For TypeScript type-only imports.","symbol":"ObjectID (type)","correct":"import type { ObjectID } from 'bson-objectid'"},{"note":"CJS still works, but ESM is preferred.","symbol":"ObjectID (CJS require)","correct":"const ObjectID = require('bson-objectid')"},{"note":"Named import is also supported and commonly used.","wrong":"import { ObjectID } from 'bson-objectid'","symbol":"ObjectID (named import)","correct":"import { ObjectID } from 'bson-objectid'"}],"quickstart":{"code":"import ObjectID from 'bson-objectid';\n\n// Generate a new ObjectID\nconst id = ObjectID();\nconsole.log(id.str); // 12-byte string\nconsole.log(id.toHexString()); // 24-character hex string\n\n// Create from hex string\nconst fromHex = ObjectID('54495ad94c934721ede76d90');\nconsole.log(fromHex.getTimestamp()); // Date object\n\n// Validate\nconsole.log(ObjectID.isValid('54495ad94c934721ede76d90')); // true\nconsole.log(ObjectID.isValid('invalid')); // false\n\n// Equality\nconst id2 = ObjectID('54495ad94c934721ede76d90');\nconsole.log(fromHex.equals(id2)); // true\n\n// Create from timestamp\nconst timeBased = ObjectID.createFromTime(1414093117);\nconsole.log(timeBased.toHexString()); // hex with zeroed rest","lang":"typescript","description":"Demonstrates ObjectID creation from different inputs, validation, equality, and timestamp extraction."},"warnings":[{"fix":"Either use ObjectID() without new or new ObjectID(); both produce same result.","message":"ObjectID() without 'new' works as a constructor but returns an immutable instance; some may expect to use 'new ObjectID()'.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure argument is either an ObjectID instance or a 24-char hex string.","message":"ObjectID.isValid accepts both ObjectID instances and 24-character hex strings; does NOT accept integer IDs or other types.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use ObjectID.createFromHexString(hex) not id.createFromHexString(hex).","message":"ObjectID.createFromHexString and ObjectID.createFromTime cannot be called as static on an instance; must be called on the ObjectID constructor itself.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use .toHexString() instead of .id for hex string.","message":"The 'id' property returns a 12-byte string; prefer 'toHexString()' for hex representation.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'api':13,51 'app':39 'browser':34 'bson':2,11,55,76 'bson-objectid':1 'cjs':71 'client':48 'compat':8 'constructor':7 'createfromhexstr':59,77 'createfromtim':58 'creation':24 'depend':23 'driver':31 'esm':69 'featur':20 'full':46,50 'generation/comparison':43 'gettimestamp':61 'hex':78 'ideal':32 'immut':63 'includ':57 'infrequ':19 'instanc':64 'isvalid':60 'javascript':72 'lightweight':4 'mongo':74 'mongodb':10,47,75 'mongodb/bson':30 'need':41 'objectid':3,6,12,28,42,56,73 'offici':54 'pariti':52 'pars':26 'provid':49 'releas':17 'server':37 'server-sid':36 'ship':65 'side':38 'stabl':16 'standalon':5 'support':68 'type':67 'typescript':66,79 'v2.0.4':14 'without':29 'zero':22 'zero-depend':21","created_at":"2026-06-05T16:55:09.900318+00:00","updated_at":"2026-06-05T16:55:09.900318+00:00","problems":[{"fix":"Use ObjectID() without new, or ensure import is correct.","cause":"Attempting to use 'new ObjectID()' with a default import that is not a constructor (though it works).","error":"TypeError: ObjectID is not a constructor"},{"fix":"Call ObjectID() with or without arguments.","cause":"Called ObjectID() without arguments but forgot to call it (e.g., ObjectID instead of ObjectID()).","error":"Cannot read properties of undefined (reading 'str')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/williamkapke/bson-objectid","github":"https://github.com/williamkapke/bson-objectid","docs":null,"changelog":null,"pypi":null,"npm":"bson-objectid","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}