12

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?

1

2 Answers 2

12

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");

Sign up to request clarification or add additional context in comments.

Comments

0

The buffer object is not available outside of Node.js, i.e in the browser. This is because (in case you were not aware) Node.js is a javascript runtime, therefore Node.js specific functionalities do not exist within a browser environment, because they are associated with the V8 engine, but not the V8 engine within a browser (note the difference here).

So essentially, uncaught refernce error: buffer is not defined means that this this doesn't exist in the browser.

https://nodejs.org/api/buffer.html#buffer_new_buffer_array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.