Browserify - How to call function bundled in a file generated through browserify in browser
Solution 1:
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.
Solution 2:
By default, browserify doesn't let you access the modules from outside of the browserified code – if you want to call code in a browserified module, you're supposed to browserify your code together with the module. See http://browserify.org/ for examples of that.
Of course, you could also explicitly make your method accessible from outside like this:
window.LogData =function(){
console.log(unique(data));
};
Then you could call LogData()
from anywhere else on the page.
Solution 3:
@Matas Vaitkevicius's answer with Browserify's standalone option is correct (@thejh's answer using the window global variable also works, but as others have noted, it pollutes the global namespace so it's not ideal). I wanted to add a little more detail on how to use the standalone option.
In the source script that you want to bundle, make sure to expose the functions you want to call via module.exports. In the client script, you can call these exposed functions via <bundle-name>.<func-name>. Here's an example:
My source file src/script.js will have this:module.exports = {myFunc: func};
My browserify command will look something like this:browserify src/script.js --standalone myBundle > dist/bundle.js
And my client script dist/client.js will load the bundled script<script src="bundle.js"></script>
and then call the exposed function like this:<script>myBundle.myFunc();</script>
There's no need to require the bundle name in the client script before calling the exposed functions, e.g. isn't necessary and won't work. <script src="bundle.js"></script><script>var bundled = require("myBundle"); bundled.myFunc();</script>
In fact, just like all functions bundled by browserify without standalone mode, the require function won't be available outside of the bundled script. Browserify allows you to use some Node functions client-side, but only in the bundled script itself; it's not meant to create a standalone module you can import and use anywhere client-side, which is why we have to go to all this extra trouble just to call a single function outside of its bundled context.
Solution 4:
I just read through the answers and seems like nobody mentioned the use of the global variable scope? Which is usefull if you want to use the same code in node.js and in the browser.
class Test
{
constructor()
{
}
}
global.TestClass = Test;
Then you can access the TestClass anywhere.
<script src="bundle.js"></script>
<script>
var test = new TestClass(); // Enjoy!
</script>
Note: The TestClass then becomes available everywhere. Which is the same as using the window variable.
Additionally you can create a decorator that exposes a class to the global scope. Which is really nice but makes it hard to track where a variable is defined.
Solution 5:
Read README.md of browserify about --standalone
parameter
or google "browserify umd"