Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use node.js buffer library in client side javascript

I would like to use buffer library (in order handle binary data) in my website. here is my use case:

const privateKey = Buffer.from('<User's private key here>', 'hex');

buffer works fine in node.js without any additional npm module or script. but somehow,it is not working in web browser.it is showing an error

uncaught refernce error: buffer is not defined

I though we need to add library script file in our html file. please help me to fix this?

like image 734
ajay Avatar asked Mar 13 '26 12:03

ajay


1 Answers

The Buffer object is only available in NodeJs and does not exist in the browser JS. But there is a script available that can be used available on GitHub.

Add standalone script to HTML from https://github.com/feross/buffer

    <script src="https://bundle.run/[email protected]"></script>

Then in JS

const privateKey = buffer.Buffer.from(PRIVATE_KEY_1, "hex");

like image 72
Ahsan Ali Avatar answered Mar 16 '26 02:03

Ahsan Ali