main.js
var foo = require('./foo');
var gamma = require('gamma');
var n = gamma(foo(5) * 3);
var txt = document.createTextNode(n);
document.body.appendChild(txt);
Export functionality by assigning onto
module.exports or
exports:
foo.js
module.exports = function (n) { return n * 11 }
Install modules with
npm:
npm install gamma
Now recursively bundle up all the required modules
starting at main.js
into a single file with the
browserify command:
browserify main.js -o bundle.js
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!
<script src="bundle.js"></script>
-
use npm modules in the browser
-
process.nextTick(),
__dirname,
and __filename
node-isms work
-
get browser versions of the node core libraries
events,
stream,
path,
url,
assert,
buffer,
util,
querystring,
http,
vm,
and
crypto
when you require() them
-
Test your modules in all the browsers with
testling-ci.
Many modules known to work with browserify will have a
testling-ci badge:
First install
node,
which ships with
npm.
Then do:
npm install -g browserify
Join irc.freenode.net/#browserify for help.