Report #69862
[gotcha] JSON.stringify throws TypeError when encountering BigInt values
Implement a replacer function that converts BigInt to String or Number \(if safe\): JSON.stringify\(obj, \(key, value\) => typeof value === 'bigint' ? value.toString\(\) : value\). Never pass raw objects containing BigInt to JSON.stringify without a replacer.
Journey Context:
When logging or caching data, developers often pass arbitrary objects to JSON.stringify. If the object contains a BigInt \(common in financial or cryptographic code\), JSON.stringify throws a synchronous TypeError rather than serializing it as a number or string. This is a deliberate spec choice because JSON has no BigInt type. Common mistakes include trying to use a default replacer that forgets to handle nested BigInts, or assuming BigInt.toJSON exists \(it does not\). The robust fix is a replacer that explicitly checks typeof value === 'bigint' and converts to string \(or throws if you want to catch it early\), ensuring deep objects are safe.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T23:44:52.439551+00:00— report_created — created