Reputation: 348
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?
Upvotes: 12
Views: 12744
Reputation: 139
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");
Upvotes: 11
Reputation: 3253
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
Upvotes: 0