How to use buffer in my nextjs #82110
Replies: 2 comments 1 reply
-
I see, IIRC, for client/browser code, this buffer is used https://github.com/feross/buffer to have node/browser compatibility, and as this issue points out, feross/buffer#318, You could polyfill the missing support for BigInt I guess, or change to try to keep the usage of this API server side exclusive - |
Beta Was this translation helpful? Give feedback.
-
Context
Why Fix (choose one) A) Run Buffer logic on the server (recommended)
// app/api/proof/route.ts
export async function POST(req: Request) {
// server-only: real Node Buffer available
const buf = Buffer.allocUnsafe(8);
buf.writeBigUInt64BE(BigInt(123));
return new Response(buf.toString('hex'));
} B) Use Web APIs on the client (no Buffer) function u64beToBytes(n: bigint) {
const ab = new ArrayBuffer(8);
const view = new DataView(ab);
view.setBigUint64(0, n, false); // false = big-endian
return new Uint8Array(ab);
}
// usage
const bytes = u64beToBytes(BigInt(123)); C) If you must polyfill Buffer in the client (not ideal) // next.config.js
const webpack = require('webpack');
module.exports = {
webpack(config) {
config.resolve.fallback = { ...(config.resolve.fallback || {}), buffer: require.resolve('buffer/') };
config.plugins.push(new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'] }));
return config;
},
}; Notes
If this fixes it, please Mark as answer so others can find it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I was build a project based on Zeroknowledge proofs
for that i need to use @aztec/bb.js package in my frontend
but i was facing the following error
buf.writeBigUInt64BE is not a function
for this i have tried to modify my nexjs config file to the below , but still i was facing that issue
Additional information
No response
Example
Beta Was this translation helpful? Give feedback.
All reactions