Search code examples
javascriptnode.jsclient

How to use node Buffer module in a client side browser - detailed explanation required please


First things first. I know that there are other questions that are similar to this e.g. use NodeJs Buffer class from client side or How can I use node.js buffer library in client side javascript

However, I don't understand how to make use of the reference to use browserify though it is given approval.

Here is my Node code:

import { Buffer } from 'buffer/';

I know this is the ES6 equivalent of require.

I would like a javaScript file implementation of this module so that I can simply use the standard html file reference:

<script src=./js/buffer.js></script>

And then use it as in for example

return new Buffer(temp).toString('utf-8');

This simply falls over with the

Uncaught ReferenceError: Buffer is not defined 

no matter how I create the buffer.js file.

So using the browserify idea I've tried using the standalone script (from the https://www.npmjs.com/package/buffer as https://bundle.run/[email protected] )

I've created a test.js file and put

var Buffer = require('buffer/').Buffer 

in it and then run browserify on it as

browserify test.js -o buffer.js

and many other variations. I'm not getting anywhere. I know I must be doing something silly that reflects my ignorance. Maybe you can help educate me please.


Solution

  • These instructions worked for me. Cheers!

    Here are the instructions you can look at the web section. https://github.com/feross/buffer

    Here is what the instructions state about using it in the browser without browserify. So from what you tried

     browserify test.js -o buffer.js
    

    I would just use the version directly that does not require browserify

    To use this module directly (without browserify), install it:

    npm install buffer

    To depend on this module explicitly (without browserify), require it like this:

    var Buffer = require('buffer/').Buffer // note: the trailing slash is important!