Search code examples
javascriptnode.jsnpmgulpbrowserify

How to compile a npm module into a browser script?


I want to customize the Parse SDK. They provide source through an npm module, which makes it usable in node environments.

How can I compile this node module into a standalone parse.js script which will run as it is on a browser?

I tried using browserify as

browserify ./parse -o parse.js

but the parse.js it spits out is quite large and still contains node remanents: process and require commands. Although it executes without any error on browser, the browser cannot find Parse definition.


Solution

  • Found the correct way to compile the parse npm module into a browser script is to use:

    browserify path/to/npm/parse --standalone parse > parse-browser.js
    

    For any other script it should be :

    browserify path_to_module --standalone global_name_to_expose > output.js
    

    Also, since the parse node module has a lot of node code, I would recommend people to use envify followed by uglify to make their code more optimized.