<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: krishnamoorthy k</title>
    <description>The latest articles on DEV Community by krishnamoorthy k (@krishnamoorthy_k_0f3f8d93).</description>
    <link>https://dev.to/krishnamoorthy_k_0f3f8d93</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2104543%2Fc2d1485d-b0a8-4e85-8683-57384a95b4d2.png</url>
      <title>DEV Community: krishnamoorthy k</title>
      <link>https://dev.to/krishnamoorthy_k_0f3f8d93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/krishnamoorthy_k_0f3f8d93"/>
    <language>en</language>
    <item>
      <title>📝 Fixing BigInt Serialization Error in Node.js + Express (A Beginner-Friendly Explanation)</title>
      <dc:creator>krishnamoorthy k</dc:creator>
      <pubDate>Tue, 18 Nov 2025 18:25:04 +0000</pubDate>
      <link>https://dev.to/krishnamoorthy_k_0f3f8d93/fixing-bigint-serialization-error-in-nodejs-express-a-beginner-friendly-explanation-5fe6</link>
      <guid>https://dev.to/krishnamoorthy_k_0f3f8d93/fixing-bigint-serialization-error-in-nodejs-express-a-beginner-friendly-explanation-5fe6</guid>
      <description>&lt;p&gt;Hey everyone! 👋&lt;br&gt;
I’m a Node.js backend developer who worked mostly with MongoDB. Recently I shifted to SQL databases — and the first unexpected challenge I faced was…&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;BigInt&lt;/strong&gt;. Yes, you heard that right.&lt;/p&gt;

&lt;p&gt;SQL databases like MySQL/PostgreSQL often use BIGINT for primary keys and timestamps, especially when dealing with large IDs or auto-increment values.&lt;/p&gt;

&lt;p&gt;Everything was fine until I fetched or created data and tried to return it through:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res.status(200).json(data);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom. ❌ Node.js threw this error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: Do not know how to serialize a BigInt

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where my debugging journey started.&lt;/p&gt;

&lt;h2&gt;
  
  
  ❗ Why Does JSON Refuse BigInt?
&lt;/h2&gt;

&lt;p&gt;Here’s the important part:&lt;br&gt;
    JSON does NOT support BigInt.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSON only supports:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;string&lt;/li&gt;
&lt;li&gt;number&lt;/li&gt;
&lt;li&gt;boolean&lt;/li&gt;
&lt;li&gt;null&lt;/li&gt;
&lt;li&gt;object&lt;/li&gt;
&lt;li&gt;array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;BigInt is not included. So when we do &lt;code&gt;res.status(200).json({data:createdUser})&lt;/code&gt; Express internally does: &lt;code&gt;JSON.stringify(data)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;…it fails because:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JSON.stringify({ id: 123n });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// ❌ Throws error&lt;/p&gt;

&lt;h2&gt;
  
  
  🔍 My First Solution (Manual Conversion)
&lt;/h2&gt;

&lt;p&gt;Initially, I wrote a utility function that walked through my object and converted any BigInt to a string.&lt;/p&gt;

&lt;p&gt;Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function convertBigInt(obj) {
  // check object values and convert BigInt → string
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this had 3 major problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Needed to call it manually on every response&lt;/li&gt;
&lt;li&gt;Nested objects became difficult&lt;/li&gt;
&lt;li&gt;Extra processing on every endpoint&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So I needed a better, global solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 The Cleanest Fix: Overriding BigInt Serialization Globally
&lt;/h2&gt;

&lt;p&gt;Here is the solution that solved everything in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setupBigIntSerialization = () =&amp;gt; {
  BigInt.prototype.toJSON = function () {
    return this.toString();
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does this do?&lt;br&gt;
   Whenever JSON.stringify() encounters a BigInt,&lt;br&gt;
it calls .toJSON() if it exists.&lt;/p&gt;

&lt;p&gt;So now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JSON.stringify({ id: 123n });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;returns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ "id": "123" }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ No error&lt;br&gt;
✔ No precision loss&lt;br&gt;
✔ No need for manual conversion&lt;br&gt;
✔ Works for all Express responses&lt;/p&gt;
&lt;h2&gt;
  
  
  📂 How to Use This in Your Project
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create a utility file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;utils/bigintSerialization.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setupBigIntSerialization = () =&amp;gt; {
  BigInt.prototype.toJSON = function () {
    return this.toString();
  };
};

export default setupBigIntSerialization;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Call this function in your entry file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;server.js&lt;/strong&gt; or &lt;strong&gt;app.js:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import setupBigIntSerialization from "./utils/bigintSerialization.js";

setupBigIntSerialization(); // enable BigInt → string globally
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Done.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now every &lt;code&gt;res.json()&lt;/code&gt;will safely serialize BigInt values without crashing your API.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚠️ Why Not Convert BigInt to Number?
&lt;/h2&gt;

&lt;p&gt;Because it’s unsafe.&lt;/p&gt;

&lt;p&gt;If you do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Number(123456789012345678901n);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will lose precision because JavaScript number uses double-precision floating point.&lt;/p&gt;

&lt;p&gt;That’s why the correct, safe approach is converting BigInt → string, not BigInt → number.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Bonus: Why This Works Internally
&lt;/h2&gt;

&lt;p&gt;This is the part that helped me fully understand what’s going on.&lt;/p&gt;

&lt;p&gt;Express uses &lt;code&gt;JSON.stringify()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;JSON.stringify() checks if the value has a .toJSON() method&lt;/p&gt;

&lt;p&gt;If yes → it uses the return value of .toJSON()&lt;/p&gt;

&lt;p&gt;So we override BigInt’s default .toJSON()&lt;/p&gt;

&lt;p&gt;Now BigInt becomes JSON-safe automatically everywhere&lt;/p&gt;

&lt;p&gt;This is the same mechanism that JavaScript uses for:&lt;/p&gt;

&lt;p&gt;Date&lt;/p&gt;

&lt;p&gt;Map&lt;/p&gt;

&lt;p&gt;Set&lt;/p&gt;

&lt;p&gt;Custom classes&lt;/p&gt;

&lt;h2&gt;
  
  
  🟢 Final Result
&lt;/h2&gt;

&lt;p&gt;After adding that one tiny helper, everything worked smoothly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No more BigInt errors&lt;/li&gt;
&lt;li&gt;No more manual conversions&lt;/li&gt;
&lt;li&gt;Cleaner backend code&lt;/li&gt;
&lt;li&gt;Safe for all routes and all responses&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎉 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;If you’re moving from MongoDB to SQL or using ORMs like Prisma, Sequelize, TypeORM, etc., you will eventually deal with BigInt values.&lt;/p&gt;

&lt;p&gt;This small utility makes your entire API BigInt-proof with almost no effort.&lt;/p&gt;

&lt;p&gt;I hope this post helps you the same way this fix helped me.&lt;br&gt;
If you have any questions or want me to write a part-2 explaining JSON.stringify and serialization in depth — comment below!&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>sql</category>
      <category>json</category>
    </item>
  </channel>
</rss>
