Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: JSON.stringify cannot serialize BigInt. Improve BigInt support (maybe). #726

Closed
adrian7 opened this issue Jan 26, 2025 · 5 comments · Fixed by #727
Closed

TypeError: JSON.stringify cannot serialize BigInt. Improve BigInt support (maybe). #726

adrian7 opened this issue Jan 26, 2025 · 5 comments · Fixed by #727
Assignees
Labels
bug Something isn't working build

Comments

@adrian7
Copy link

adrian7 commented Jan 26, 2025

Hello I'm a newbie to typecript and decided to give brisa a try. After stumbling on some issues regarding to my lack of skill, today I had the misfortune to stumble across a very weird one:

brisa dev
[ info ]   🚀 Brisa 0.2.3: Running on Bun.js 1.1.43
[ wait ]  starting the development server...
[ wait ]  compiling 6 server entrypoints...
[ wait ]  compiling server actions...
[ wait ]  analyzing and preparing client build...
[ error ]   Ops! Error:
[ error ]   --------------------------
[ error ]   Failed to build
[ error ]   JSON.stringify cannot serialize BigInt.
[ error ]   --------------------------
[ error ]   TypeError: JSON.stringify cannot serialize BigInt.
    at stringify (unknown)
    at Pa (/home/moustacios/Workspace/Aivatar.Art/app/node_modules/brisa/out/cli/build.js:104:1955)
    at processTicksAndRejections (native:7:39)

This happened after I have added the mongodb lib and written a function to return the result of .insertOne() as follows:

export async function saveUser(
    payer: UserDetails,
    transactionId:string
) {
    const user:UserAccount = {
        ....
    };

    //(!) TODO cannot return here due to error
    return await UsersCollection.insertOne(user);
}

After some research on stack overflow, I found a suggested solution here: GoogleChromeLabs/jsbi#30 (comment)

I added, the file src/types/global.d.ts and copied the declaration to src/pages/index.tsx. But nothing changed... . So I went around anc copied the declaration randomly at the top of other files hoping for misjudgment on my side.
Finally when I copied the declaration in src/middleware.ts the error went away 😃 .

I was really puzzled on the solution but glad I can continue.

The solution I'd like

I would like bigint to be supported by default, and not have to deal with these issue in future versions.

Alternative solutions I've considered

I was pretty annoyed of the fact that bun itself, does not complain about BigInt, but Brisa does.
So was considering writing an API separate of the brisa app, only running on bun, or switching to another framework.

Thanks.

@aralroca
Copy link
Collaborator

@adrian7 In order to better debug where this serialization is occurring, could you please report a minimal reproducible example to make sure we solve this? Thanks a lot and thanks to report it!

@aralroca aralroca self-assigned this Jan 26, 2025
@aralroca aralroca added bug Something isn't working build labels Jan 26, 2025
@aralroca
Copy link
Collaborator

Ok for the line of the crash, looks this JSON.stringify of the server AST:

JSON.stringify(ast, (key, value) => {

@aralroca
Copy link
Collaborator

Ok, it's easy to reproduce it, is not necessarily a reproducible repo example @adrian7.

Image

@aralroca
Copy link
Collaborator

@adrian7 is fixed on 0.2.4-canary.4

@adrian7
Copy link
Author

adrian7 commented Jan 27, 2025

Wow thanks for the help @aralroca 👍🏽 . Keep up the good work guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working build
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants