browser-require

The easiest way to require and use CommonJS and NPM modules from your browser

Npm makes it easy to share code on your server. But how many times have you
wanted to share Javascript code easily between the server and the browser?
browser-require allows you to easily use CommonJS and NPM modules defined
on the server in your browser.

browser-require enables you to require both relative (local to your project)
CommonJS modules as well as global NPM modules.

Installation

To install:
$ npm install browser-require

Using browser-require within your connect app

Currently, browser-require depends on the
connect middleware framework,
if you want to serve client javascript files that contain requires.

// The following line "app.use(..." is what you want to add to your project
// Make sure the browser-require middleware comes before staticProvider middleware
app.use(exposeRequire({
base: __dirname // This is where we look to find your non-global modules
});
app.use(connect.staticProvider(__dirname));
app.listen(3000);

How it works

The server looks up the source and its module dependencies (if any) recursively.

Once the server has collected all dependencies, it compiles the top-level file plus
its dependencies into a file that gets sent back to the browser.

Command line binary

Sometimes you need to statically compile a set of javascript client files from the command line.
For example, this is necessary if you are building a Chrome plugin. A Chrome plugin can use JavaScript
files that exist inside the Chrome plugin (as opposed to fetching a JavaScript file that exists on the
server). Therefore, it is necessary in this case to compile your JavaScript files and their dependencies
outside of the context of a server.

browser-require supports this via a command line binary. You can use it in the following way: