Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browserify - Exposing A Method on the Client Side

I guess I'm missing something with Browserify. I want to browserify my custom node module, which exposes a couple of functions. How would I do that?

Every example I've seen in browserify it's always a console.log or an alert. It's not good to me if it runs immediately. I want to run my browserify code on demand.

But for the sake of this example. Say I export a sum method. Take this example:

module.exports = {
  sum = function(a, b) {
    return a + b;
  }
};

After I browserify this via the standard command: browserify index.js -o bundle.js.

Then I add this file to the my index.html.

Within my AngularJS application or any client side code. How do I access this sum method?

I can't find it on window.sum. Or maybe I got it all wrong.

like image 349
Chester Rivas Avatar asked Sep 01 '15 18:09

Chester Rivas


1 Answers

@WellDone2044 answers should give you the idea how to solve your problem. When you run browserify as shown by WellDone2044, it will bundle your javascript files base on your main file which in the example is index.js. The bundled output will be written to bundle.js and browserify will include Node's require function. Therefore, you'll be able to use the require method on your client side scripts. Note that you must always run browserify on each modification you made to your js scripts. You're index file might look like this:

var sum = require('./sum.js');

console.log(sum(1,2)); // displays 3
like image 135
ist Avatar answered Oct 03 '22 12:10

ist