search
Javascript

Fix: Error: Cannot serialize a BigInt error

Quick fix for 'Error: Cannot serialize a BigInt' error. Learn how to properly handle BigInt serialization in JavaScript applications.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 2 min read
JavaScript BigInt Serialization JSON Error Fix

The ‘Error: Cannot serialize a BigInt’ error occurs when trying to serialize BigInt values to JSON or other formats that don’t support BigInt natively, as JSON doesn’t have a native BigInt type.


How the Error Happens

❌ Error Scenario:

// ❌ This causes the error
const data = {
  id: 1234567890123456789012345678901234567890n, // ❌ BigInt value
  name: 'Big Number'
};

JSON.stringify(data); // ❌ Error: Cannot serialize a BigInt

✅ Quick Fix - Handle BigInt Serialization

Solution 1: Custom Replacer Function

// ✅ Use custom replacer to handle BigInt
const data = {
  id: 1234567890123456789012345678901234567890n,
  name: 'Big Number'
};

const jsonString = JSON.stringify(data, (key, value) =>
  typeof value === 'bigint' ? value.toString() : value
);

console.log(jsonString); // ✅ Serializes BigInt as string

Solution 2: Reviver Function for Deserialization

// ✅ Parse back to BigInt using reviver
const jsonString = '{"id":"1234567890123456789012345678901234567890","name":"Big Number"}';

const parsed = JSON.parse(jsonString, (key, value) =>
  /^\d+n$/.test(value) ? BigInt(value.slice(0, -1)) : value
);

console.log(parsed.id); // ✅ BigInt value restored

Solution 3: Convert BigInt Before Serialization

// ✅ Convert BigInt to string before serialization
function convertBigInt(obj) {
  return JSON.parse(JSON.stringify(obj, (key, value) =>
    typeof value === 'bigint' ? value.toString() : value
  ));
}

const data = {
  bigId: 9007199254740991n, // Larger than Number.MAX_SAFE_INTEGER
  name: 'Test'
};

const serializableData = convertBigInt(data);
const json = JSON.stringify(serializableData); // ✅ Works fine

Solution 4: Use Alternative Serialization Libraries

// ✅ Use libraries that support BigInt
// npm install lossless-json
import { stringify, parse } from 'lossless-json';

const data = {
  bigNumber: 123456789012345678901234567890n
};

// ✅ Serialize with BigInt support
const losslessJson = stringify(data);
const parsedData = parse(losslessJson); // ✅ BigInt preserved
Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

Javascript

How to Resolve Dynamic import callback was not specified error

Quick fix for 'Dynamic import callback was not specified' error. Learn how to properly handle dynamic imports in JavaScript applications.

January 8, 2026
Javascript

Fix: ESM packages need to be imported error

Quick fix for 'ESM packages need to be imported' error. Learn how to properly import ES modules in Node.js and JavaScript environments.

January 10, 2026
Javascript

Fix: Error when starting dev server: port already in use error

Quick fix for 'Error when starting dev server: port already in use' error. Learn how to resolve port conflicts in JavaScript development servers.

January 8, 2026