Browserify

With Browserify we can use Node modules directly in the browser. This means that you can build your projects with the power of the npm package manager and the Node module syntax exposed in the previous sections. Then Browserify can take your source code and apply some transformations to be able to run your code in the browser environment.

A very simple module that exposes an object with a method that prints a hello message can be written as a Node module:

// hello.js
module.exports = {
  sayHello: function(name) {
    name = name || 'world';
    console.log('hello', name);
  }
}

This simple piece of code can be loaded from another script as shown next:

// main.js var hello = require('./hello'); hello.sayHello(); // hello world hello.sayHello('abiee'); ...

Get Mastering Backbone.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.