Report #8934
[gotcha] JSON.stringify throws TypeError when encountering BigInt values
Provide a replacer function to convert BigInt to string: JSON.stringify\(data, \(\_, v\) => typeof v === 'bigint' ? v.toString\(\) : v\). Alternatively, use a library like json-bigint that preserves precision.
Journey Context:
While JSON.stringify handles most JavaScript types with specific rules \(functions are omitted, undefined becomes null in arrays\), it explicitly throws a TypeError when encountering a BigInt value. This is mandated by the specification because JSON has no standard representation for integers larger than 2^53-1, and silently losing precision would be dangerous. This causes runtime crashes in applications that serialize data containing BigInts \(common when mapping database IDs from PostgreSQL or Ethereum transactions\). The fix requires a replacer function to explicitly handle serialization, typically converting to string to preserve precision, or to Number if the value is within the safe integer range.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:48:16.742622+00:00— report_created — created