npm / bson
bson
What changed in bson 2 to 7, from its official release notes, upgrade guides and advisories. Sources checked 2026-12-05.
34 breaking3 security3 deprecations10 new APIs and options1 performance4 behavior and defaults3 tooling23 fixes and other
See which of these apply to your code, ranked, in your agent:
npx @unstale/cli initbson 731 changes
Breaking changes 8
On malformed input containing a string element with a declared length of zero, the experimental on-demand parser now throws a BSONOffsetError instead of hanging.
Throws an exception when a circular reference is detected instead of hanging indefinitely.
A Code with Scope scope containing $ref, $id, and $db is returned as a plain object instead of being promoted to a DBRef instance.
ByteUtils and NumberUtils are no longer available from the onDemand namespace; they have moved to the top-level package export.
BSON now uses JavaScript BigInt syntax and requires a JavaScript engine that supports BigInt literal syntax.
The minimum supported Node.js version is now 20.19.0; Node.js 16 and 18 are no longer supported.
The ObjectId constructor no longer accepts a number; use ObjectId.createFromTime() to create an ObjectId from a numeric timestamp.
BSON no longer includes atob, btoa, and TextEncoder polyfills for React Native builds because Hermes supports them natively.
Security 2
Counts negative zero as an 8-byte double, matching serialize() and preventing undersized buffers and out-of-bounds errors.
Counts a Code value with an empty scope as code with scope, matching serialize() and preventing undersized buffers and out-of-bounds errors.
Deprecations 2
Long methods and operations that are inapplicable to Timestamp are now marked deprecated; use the .t and .i accessors for its timestamp components.
The BSON Binary subtype 2 constant is now deprecated to align with the BSON specification.
New APIs and options 5
ByteUtils now provides a copy() method that behaves identically to Node.js Buffer.copy().
EJSON.stringify and EJSON.serialize accept ignoreUndefined to skip keys whose values are undefined; it defaults to false.
ByteUtils is now a public top-level export providing platform-agnostic tools to manipulate binary data; it is experimental and may change.
NumberUtils is now exported from the BSON package for reading to and from buffers of bytes; it is experimental and may change.
BSON value classes now expose a bsonType symbol property as an alias for _bsontype, distinguishing wrapper instances from documents with arbitrary keys.
Performance 1
Stores its 12 bytes as four inline integers instead of a Uint8Array, cutting per-ObjectId memory by about 63% with no serialization slowdown.
Behavior and default changes 1
All BSON bundles now use globalThis.crypto for random byte generation; the Node.js bundle no longer requires crypto.
Tooling 2
isUint8Array now uses a type predicate.
The TypeScript target has been updated to ES2023.
Fixes and other changes 10
Guards against optional APIs implemented with throwing stubs, preventing import-time throws in Next.js Edge and Bun runtimes.
Returns accurate byte counts for Int32 and BSONSymbol values, correcting a previous 12-byte overcount per value.
Properly accounts for ES Map objects when calculating size; serialization was not affected.
Avoids crypto calls during module initialization, which are forbidden by Cloudflare Workers and other environments.
Uses the first instant of year 10000 as the relaxed EJSON date bound instead of a date five hours after the maximum limit.
ObjectId startup snapshot API calls now use optional chaining, preventing a TypeError in environments with a stubbed or partial process global.
The constructor coerces subtype inputs to match BSON serialization, keeping the subtype property consistent with the serialized value.
Serialization, deserialization, and object-size calculation use iterative algorithms, so sufficiently nested documents no longer exhaust the call stack.
The space parameter is no longer discarded when stringify is called with value, options, and space, enabling formatted output for that signature.
Reverts a breaking change to the onDemand namespace, restoring compatibility with v7.0.0 of the MongoDB Node.js Driver.
bson 69 changes
Breaking changes 7
The constructor and write() no longer accept strings as binary sequences; string-format interpretation is expected outside the Binary class.
value() now returns only a properly sized Uint8Array or Buffer containing the data relevant to the Binary value.
The major version is now 6; only BSON objects with the same major version can be serialized, and mismatches throw BSONVersionError.
The constructor and fromString now throw a BSONError when conversion would lose precision instead of silently rounding.
The minimum supported Node.js version is now v16.20.1.
The constructor no longer interprets strings of length 12 as UTF8 bytes that can be converted to an ObjectId.
The deprecated, unused property was removed.
Fixes and other changes 2
Inspection now accounts for quotes when inspecting Code and BSONSymbol.
The method now accepts undefined and null parameters for equality checks.
bson 527 changes
Breaking changes 12
BSON errors when given BSON types that are not from this version.
All thrown errors are now BSONErrors.
A BSONType enum is added, while internal constants are removed from the export.
The Decimal128 toObject transformer is removed.
Extended JSON double values no longer include a .0 suffix.
The evalFunctions option is removed.
The minimum Node.js requirement is now v14.20.1 or later; older EOL Node.js versions are no longer supported.
Deprecated ObjectId methods are removed.
The capital-D ObjectID export is removed.
Enumeration now includes only own properties.
The serializer now validates its root input.
The Timestamp constructor now validates its argument.
New APIs and options 5
Set useBigInt64 to true to return BSON int64 values as BigInt; this is not enabled by default.
EJSON.parse now supports parsing $numberLong values to BigInt when useBigInt64 is enabled.
Set useBigInt64 to true to parse int64 values as BigInt; this is not enabled by default.
BSON now offers a native ESM bundle that works in Node.js and the browser alongside the existing CommonJS format.
Package exports are added for React Native.
Behavior and default changes 3
BSON.serialize now converts BigInt values into BSON Long values.
EJSON.stringify now converts BigInt values to EJSON's $numberLong format.
BSON's cross-platform byte handling uses Uint8Array on the web and Buffer in Node.js, allowing its ES module build to run without shims or polyfills.
Tooling 1
The TypeScript target version is raised to es2020.
Fixes and other changes 6
Canonical EJSON double precision accuracy is fixed.
EJSON parsing supports the $numberDouble: -0.0 representation of negative zero.
EJSON stringification preserves negative zero using the $numberDouble: -0.0 representation.
Serialization now checks for a toBSON method on values in Map entries.
Negative zero is serialized as a BSON double.
serializeFunctions no longer breaks function names outside basic Latin.
bson 48 changes
Breaking changes 7
BSON is no longer an instantiable type; use the exported namespace to call methods such as BSON.serialize.
Types no longer maintain circular references; BSON.Timestamp.Timestamp is no longer available.
The Symbol type is renamed BSONSymbol to avoid conflicts with the ES6 Symbol.
The _bsontype property of all known types is now non-enumerable.
Extended JSON is merged into bson and is accessible through the EJSON export, including stringify and parse.
Doubles are deserialized as Number when relaxed mode is enabled.
Node.js v6 or above is now required because the module uses ES2015 features in non-transpiled code.
Deprecations 1
Strict mode is deprecated in favor of relaxed mode, in line with the Extended JSON specification.
bson 26 changes
Security 1
Added a basic guard against REDOS attacks in Decimal128 handling.
Fixes and other changes 5
Fixed the browser property in package.json, affecting how browser-aware tooling resolves the package.
Only objects with allowed $keys are upgraded to DBRefs.
toString and fromString now correctly handle cases with too many significant digits.
When pid is 1, ObjectId generation uses a random value.
Documents are now written to the start of the intermediate buffer.