1

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?

1 Answer 1

2

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..
Sign up to request clarification or add additional context in comments.

7 Comments

I did like you said, but I got "Uncaught Error: Cannot find module 'peer-file'". Maybe have I create the bundle typing: browserify -r ./index.js:peer-file > build.js?
I followed this: github.com/substack/node-browserify#external-requires. So the noun after the : means the name associated to the exported module. Am I right?
Does your project have a package.json (generated with npm init) and you have the module listed there in dependencies and it is installed into a node_modules folder?
Yes, but after that, I need to move the bundle.js file into the public folder - I'm working with express. Anyway, the answer is yes.
I recommend you use grunt or gulp to set up your build task. I also recommend you use webpack instead of browserify as it is much faster at compiling. Setting up a build task in grunt or gulp for this is simple - can't see exactly what you are doing wrong without seeing your project though
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.