Browserify - create bundle with external module

泄露秘密 提交于 2019-12-13 19:48:14

问题


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:


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..


来源:https://stackoverflow.com/questions/26672071/browserify-create-bundle-with-external-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!