最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked May 21, 2019 at 11:20 ajayajay 3482 gold badges7 silver badges19 bronze badges 1
  • See github./feross/buffer – Arto Bendiken Commented Mar 31, 2021 at 22:08
Add a ment  | 

2 Answers 2

Reset to default 11

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./feross/buffer

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

Then in JS

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

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/api/buffer.html#buffer_new_buffer_array

发布评论

评论列表(0)

  1. 暂无评论