Search code examples
javascriptnpmbrowserify

Browserify - create bundle with external module


I'm really new to browserify world. I want to use this module peer-file, in order to allow the file transfer between two browsers. Reading the Usage section into readme, I note I have to include the script bundle.js in my web page. To build the bundle I need to type browserify -r ./index.js > build.js, where -r option means external require, so I can use in my main script the keyword require(), like this:

var send = require('peer-file/send')
var receive = require('peer-file/receive')

However, when I load the web page, I receive this error into the console. Uncaught Error: Cannot find module 'peer-file/send'

Any suggestion?


Solution

  • If you look at the index file - https://github.com/michaelrhodes/peer-file/blob/master/index.js

    It adds send and receive to the exports. So you first get a handle to that, then you can access the exports with dot notation.

    var send = require('peer-file').send;
    var receive = require('peer-file').receive;
    

    Or just get it once:

    var peerFile = require('peer-file');
    
    // Later
    peerFile.send..
    peerFile.receive..