Lumination Lumination - 9 months ago 32
Node.js Question

not able to bundle js

So I'm trying to make use of the "simple-peer" library, but on their Github it says you need to bundle it using browserify. So I installed browserify, downloaded the library via npm (which created a folder named node_modules), created a folder named js in the same folder as node_modules is located and a main.js file in it containing the following:



var _ = require('core-util-is');
var _ = require('debug');
var _ = require('get-browser-rtc');
var _ = require('inherits');
var _ = require('isarray');
var _ = require('ms');
var _ = require('process-nextick-args');
var _ = require('randombytes');
var _ = require('readable-stream');
var _ = require('safe-buffer');
var _ = require('simple-peer');
var _ = require('string_decoder');
var _ = require('underscore');
var _ = require('util-deprecate');





Now from what I've read online I thought all I had to do was to type:

browserify js/main.js -o js/bundle.js

and it should automatically merge all of the code into one bundled file, but everytime I try to run it I just get an empty file called "bundle.js.tmp-browserify-52350507248623401324" and the task never finishes. I've tryed to look up noumerous tutorials but they all say the same and I don't know what I'm doing wrong.

Any help is greatly appreciated, thanks in advance

Answer Source

The node_modules folder is for dependencies that you install by npm. Your code should be in the parent folder. So let's say you have a folder myapp. In there you put your code, e.g. main.js. And in that folder you have your subfolder node_modules as created by npm. Be sure to run

npm install --save simple-peer

this will install the package to node_modules and add the dependency to package.json.

Now if you run browserify in your myapp folder, it will look for dependencies you have required in your code and build it into your bundle.

Make sure your code includes only the direct dependencies, i.e. it will be enough to require simple-peer in your code. All the dependencies from simple-peer will be found by browserify.

You can try the example I posted as this gist: https://gist.github.com/aldafu/3a053353a71bb518bce8f4c66c868bcb

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download