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.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With