Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browserify - How to call function bundled in a file generated through browserify in browser

I am new to nodejs and browserify. I started with this link .

I have file main.js which contains this code

var unique = require('uniq');  var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  this.LogData =function(){ console.log(unique(data)); }; 

Now I Install the uniq module with npm:

 npm install uniq 

Then I bundle up all the required modules starting at main.js into a single file called bundle.js with the browserify command:

browserify main.js -o bundle.js 

The generated file looks like this:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ var unique = require('uniq');  var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  this.LogData =function(){ console.log(unique(data)); };  },{"uniq":2}],2:[function(require,module,exports){ "use strict"  function unique_pred(list, compare) {   var ptr = 1     , len = list.length     , a=list[0], b=list[0]   for(var i=1; i<len; ++i) {     b = a     a = list[i]     if(compare(a, b)) {       if(i === ptr) {         ptr++         continue       }       list[ptr++] = a     }   }   list.length = ptr   return list }  function unique_eq(list) {   var ptr = 1     , len = list.length     , a=list[0], b = list[0]   for(var i=1; i<len; ++i, b=a) {     b = a     a = list[i]     if(a !== b) {       if(i === ptr) {         ptr++         continue       }       list[ptr++] = a     }   }   list.length = ptr   return list }  function unique(list, compare, sorted) {   if(list.length === 0) {     return []   }   if(compare) {     if(!sorted) {       list.sort(compare)     }     return unique_pred(list, compare)   }   if(!sorted) {     list.sort()   }   return unique_eq(list) }  module.exports = unique },{}]},{},[1]) 

After including bundle.js file into my index.htm page, how do I call logData function ??

like image 523
SharpCoder Avatar asked Apr 25 '14 14:04

SharpCoder


People also ask

Is Browserify a dev dependency?

We can also include browserify itself as a dependency, however it isn't a dependency for the project to run – any user to our app can find Superman without needing to run Browserify. It is one of our devDependencies – modules required for developers to make updates to this app.

How do I use Browserify bundles?

Bundle up your first module With Browserify you can write code that uses require in the same way that you would use it in Node. Browserify parses the AST for require() calls to traverse the entire dependency graph of your project. Drop a single <script> tag into your html and you're done!

What is Browserify in NPM?

Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node. js-style modules that compile for use in the browser. Browserify. Developer(s) Browserling team and Browserify contributors.


1 Answers

The key part of bundling standalone modules with Browserify is the --s option. It exposes whatever you export from your module using node's module.exports as a global variable. The file can then be included in a <script> tag.

You only need to do this if for some reason you need that global variable to be exposed. In my case the client needed a standalone module that could be included in web pages without them needing to worry about this Browserify business.

Here's an example where we use the --s option with an argument of module:

browserify index.js --s module > dist/module.js 

This will expose our module as a global variable named module.
Source.

Update: Thanks to @fotinakis. Make sure you're passing --standalone your-module-name. If you forget that --standalone takes an argument, Browserify might silently generate an empty module since it couldn't find it.

Hope this saves you some time.

like image 173
Matas Vaitkevicius Avatar answered Sep 17 '22 04:09

Matas Vaitkevicius