Report #73786
[gotcha] JSON.stringify throws TypeError when encountering BigInt values
Never pass BigInt directly to JSON.stringify. Use a replacer function to convert BigInt to String: \`JSON.stringify\(data, \(key, value\) => typeof value === 'bigint' ? value.toString\(\) : value\)\`. Alternatively, convert BigInt to Number before serialization if it fits safely, or use a library like \`json-bigint\` for preserving precision.
Journey Context:
BigInt was added to JavaScript to handle arbitrary-precision integers, but the JSON specification does not support BigInt as a distinct type \(JSON numbers are IEEE 754 doubles\). The ECMAScript spec explicitly states that serializing a BigInt must throw a TypeError. This catches developers off-guard because BigInt behaves like a primitive in most contexts \(typeof returns 'bigint'\), but unlike Number, it cannot be implicitly converted to JSON. Common mistakes include trying to serialize database records containing BigInt IDs \(common in PostgreSQL bigint columns\), which crashes the serialization. The fix requires explicit conversion to String \(losing numeric type in JSON\) or Number \(risking precision loss for values > 2^53\), or using specialized libraries.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T06:26:45.497945+00:00— report_created — created