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?
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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.