Derek Derek - 1 year ago 84
Node.js Question

using a module exported from browserify

I have a simple node.js project where I ran the following commands:

npm init
npm install buffer --save
browserify -r buffer -o buffer.js

How can I require that buffer.js file into other files or projects, and actually use the Buffer classed contained within it?

I have tried

var Buffer = require('./buffer.js');
var x = new Buffer();

but I get
TypeError: Buffer is not a constructor

What am I doing wrong to be able to use that node module from another location?

Answer Source

Generate a UMD bundle with the -s flag:

browserify -r buffer -o buffer.js -s buffer

And then fix the file in which you require it:

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

var x = new Buffer('some content');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download