Modular JavaScript development with Browserify and LivePage
Posted on February 5, 2014 by Paul
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:
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, e.g. jQuery.getScript(). More recently, like a few years ago, RequireJS become the state of the art approach. With RequireJS you would write:
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:
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:
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:
Now, we are ready to use require in our JavaScript files. Let’s start with a simple example:
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:
save the above as main.js. We can compile the above two files using browserify through watchify:
Let’s include bundle.js in a dummy html file:
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:
Now, add an extra line to main.js and save the file:
the output file, bundle.js, will be generated for you, if you refresh the test.html in your browser, you should see:
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:
and the result, assuming LivePage was enabled:
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:
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 8 the Right Way by J. R. Wilson: