Reputation: 333
I am trying to use simple-peer package as below:
var Peer = require('simple-peer');
var peer = new Peer({});
peer.on('signal', function(data) {
console.log(JSON.stringify(data));
});
I get nothing printed to the console and the below error appear:
SCRIPT5009: 'Buffer' is undefined
NOTE: My project is chat application that works fine when sending text messages, but after I add the above code the chat doesn't work as well.
I am using other npm packages such as socket.io and they are working fine.
What am I doing wrong here?
Upvotes: 2
Views: 67
Reputation: 5671
Because Buffer is a Node global for server-side use, you need to include a polyfill to use the same library on the client.
Meteor makes this easy with the meteor-node-stubs
package. Install it with:
meteor npm install --save meteor-node-stubs
Note that these are not small and may significantly increase your bundle size. Keep an eye out for how much impact it has so you can decide if the use of this particular package is worth the added weight.
To make sure it's available to the module that's expecting it, you might need to add it to the window
before initializing simple-peer
:
window.Buffer = require('buffer').Buffer
Add this to your main js file (or whichever is the first to run)
Upvotes: 2
Reputation: 7777
According to the nodejs buffer documentation...
The Buffer class is a global within Node.js, making it unlikely that one would need to ever use require('buffer').Buffer.
but you might need to add a line
var Buffer = require('buffer').Buffer
To make it available to this module. Meteor is not on the most current version of node - upgrading to 1.6 may also solve the problem.
Upvotes: 1