With https://github.com/substack/node-browserify you can work with buffers in the Browser by using: https://github.com/toots/buffer-browserify. However: this can be very slow in the browser: For faster access use https://github.com/chrisdickinson/bops
Answer from Martin Heidegger on Stack OverflowWith https://github.com/substack/node-browserify you can work with buffers in the Browser by using: https://github.com/toots/buffer-browserify. However: this can be very slow in the browser: For faster access use https://github.com/chrisdickinson/bops
There is no direct support for Buffer in browser-based JavaScript, and I am not aware of any compatibility library that implements the Buffer API (yet).
The equivalent functionality in the browser is provided by TypedArrays. You can learn about them here:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
When porting a Node Buffer-based implementation to browser-based JavaScript, I found these answers helpful:
- Converting between strings and ArrayBuffers
- Javascript - Converting between Unicode string and ArrayBuffer
» npm install buffer
» npm install browser-buffer
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");
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
Buffer is available in Node.js runtimes, but not in browsers.
Without knowing the exact shape of your build system I can only give general advice, which is that you'll need to install a Buffer polyfill like https://github.com/feross/buffer.
We're actively looking to eliminate Buffer from @solana/web3.js completely.
I can't comment yet so... I'll just leave this here since it's relevant. Buffer is not defined