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

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.

Leave a Comment