Modular JavaScript development with Browserify and LivePage
Posted on February 5, 2014 by Sol

Working on a medium to large scale JavaScript project was always a challenge. Just picture yourself editing a few thousands lines of code in a single source file! Fortunately, the next version of the JavaScript standard, ECMAscript 6, is going to give us modules. Personally, I think that the golden standard for JavaScript modules is the Node.js model, what could be more elegant than to write:

1
require('foo');

and instantly have access to everything was exported in the required module ?

In the past, a possible solution to the above problem was to use an AJAX call to load a piece of JavaScript code in another file, for e.g. jQuery.getScript(). More recently, like a few years ago, RequireJS become the state of the art approach. With RequireJS you would write:

1
2
3
require(["foo"], function(foo) {
	//This is called when foo.js is loaded.
}

this gives us a callback function that will fire once the required module is loaded. But, what if you want something even simpler, something that looks more like the Node.js syntax, or Lua’s require, or other languages ? Enter Browserify!

With Browserify you can use a simple syntax and avoid the use of an extra callback function. Basically, Browserify works like a compiler, you specify a list of input JavaScript files and they will be combined in a single file, something like:

1
browserify input_1.js input_2.js -o bundle.js

Obviously, it will be rather tedious to use the above command each time you change something in one of your JavaScript input files. We could use watchify to watch for changes and run browserify for us. Watchify uses the same syntax as browserify:

1
watchify input_1.js input_2.js -o bundle.js

Now, every change in one of the input JavaScript files will trigger a rebuild of the output, bundle.js file.

You can install browserify and watchify with the NodeJS manager npm. Assuming you have installed NodeJS on your computer, open a Terminal (for Windows you could use a Command Prompt, just remove the sudo from both lines) and write:

1
2
sudo npm install -g browserify
sudo npm install -g watchify

Now, we are ready to use require in our JavaScript files. Let’s start with a simple example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function Person(first_name, last_name, age) {
	this.first_name = first_name;
	this.last_name = last_name;
	this.age = age; 
}

Person.prototype.getName = function() {
	return this.first_name + " " + this.last_name;
}

Person.prototype.getAge = function() {
	return this.age;
}

module.exports = Person;

Please note the highlighted line from above, this will let us use the object Person in a different file. Save the above file as person.js.

Now, we could use the above code in a separate JavaScript file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
(function() {
	var Person = require('./person.js');

	// create and use two persons
	var person_1 = new Person("William", "Adama", 65);
	var person_2 = new Person("Lee", "Adama", 30);

	console.log(person_1.getName());
	console.log(person_2.getName());	
})();

save the above as main.js. We can compile the above two files using browserify through watchify:

1
watchify person.js main.js -o bundle.js

Let’s include bundle.js in a dummy html file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!DCCTYPE html>
<html>
	<head>
		<title>Using Browserify</title>
	</head>

	<body>
		<script src = "bundle.js"></script>
	</body>
</html>

save the above as test.html and open it in a browser. If you open the JavaScript console from your browser developer tools you should see something like this:

figure 1

Now, add an extra line to main.js and save the file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
(function() {
	var Person = require('./person.js');

	// create and use two persons
	var person_1 = new Person("William", "Adama", 65);
	var person_2 = new Person("Lee", "Adama", 30);

	console.log(person_1.getName());
	console.log(person_1.getAge());
	console.log(person_2.getName());	
})();

the output file, bundle.js, will be generated for you, if you refresh the test.html in your browser, you should see:

figure 2

What if you want to automatically refresh your webpage when one of the input JavaScript is changed ? If you use Google Chrome there is an extension for that, LivePage. Enable LivePage and each time you will change one of your JavaScript files the page will be reloaded. For e.g. add an extra line at the end of main.js:

1
2
3
4
5
(function() {
	...
	console.log(person_2.getName());	
	console.log(person_2.getAge());
})();

and the result, assuming LivePage was enabled:

figure 3

If you want to be able to read bundle.js or to see where an error has occurred in the original JavaScript files, use the –debug flag:

1
watchify --debug person.js main.js -o bundle.js

The above will enable source maps for your input files.

If you want to learn more about JavaScript, I would recommend reading Secrets of the JavaScript Ninja by J. Resig and B. Bibeault:

If you are interested in Node.js, this is a good book Node.js the Right Way by J. R. Wilson:

blog comments powered by Disqus