Skip to main content
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Mark Drew
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Mark Drew ( @markdrew )

My First Look At The RequireJS Build Optimizer For Node.js

Published in Comments (11)

Over the last few weeks, I've started to look into the RequireJS asynchronous loader and JavaScript dependency management system. Right off the bat, the facilitated modularity and code organization provided by RequireJS feels like a really solid approach to thick-client application development. With the increased modularity, however, we end up with many small, cohesive files. And, while this is great for development and debugging, it's not so great for production where a large number of HTTP requests can result in slower page-loads. To get the best of both worlds, RequireJS provides a build tool that will concatenate and inline dependencies to produce a single optimized JavaScript file for your application.

As with all of my blog posts on RequireJS, this is my first exploration of a particular feature. As such, it's not an in-depth, exhaustive exploration; rather, it's a small glimpse at the power provided by the optimization tool. To experiment with the build tool - r.js - I created a small, pointless JavaScript application that does nothing more than link a few dependencies together. Here is the main HTML file that gets loaded:

test.htm (Our Application User Interface)

<!DOCTYPE html>
	<title>Using The RequireJS Optimizer With Node.js</title>

	<!-- Load the RequireJS + jQuery library. -->

	<!-- Left intentionally blank. -->

As you can see, this does nothing more than load the RequireJS / jQuery bundle and define "main.js" as the core application file. It's important that main.js is external to the HTML page so that the build tool can easily parse it and minify it.

Here is the main.js file. It does nothing more than require a few dependencies and log some success statements to the JavaScript console.

main.js (Our Main Application File)

// Require some modules. Notice that we are using the TEXT plugin
// with this set of dependencies.
	function( data, mod1, mod2 ){

		// Log the loaded modules.
		console.log( "Loaded!" );
		console.log( "Data:", data );
		console.log( "Mod-1:", mod1 );
		console.log( "Mod-2:", mod2 );


As you can see, the main file makes use of both the Text plugin (text!) and some standard JavaScript modules. The build tool, as part of the optimization process, will inline the content of the "data.txt" file so that the text value does not require any additional HTTP requests.

Here are the modules being loaded.


This is text from the data.txt file.


// Define an anonymous module.
	function( subsequent ){

		return( "This is mod-1 [" + subsequent + "]" );



// Define an anonymous module.
	function( subsequent ){

		return( "This is mod-2 [" + subsequent + "]" );


Both of the included JavaScript modules have a dependency on the subsequent.js module. I put this in to make sure that nested dependencies would be parsed as well.

subsequent.js (Our Nested Module Dependency)

// Define a subsequent anonymous dependency that is required
// by the other modules.

		return( "SUB-MODULE" );


So that's all the code in this application. As you can see, it's a rather trite example; the only thing we're trying to test here is the optimizer, not the architecture of the application. When I run the main HTML file, however, I am able to get a successful outcome with the following values logged to the console:

Data: This is text from the data.txt file.
Mod-1: This is mod-1 [SUB-MODULE]
Mod-2: This is mod-2 [SUB-MODULE]

If you were to look at the Firebug Network activity for this page request, you would see that RequireJS made a separate request for every module (and text-file) dependency in the application:

HTTP requests needed before the RequireJS build and optimization process.

Ok, so now let's take a look at the build process. To optimize the JavaScript files, I've got the r.js file saved in the directory above my application directory. Then, using the command line tool (Terminal on the Mac) I navigated to the application directory and ran the following command:

ben$ node ../r.js -o name=main out=main-built.js baseUrl=.

This will run the "r.js" file through my local version of Node.js (v0.4.8). During the build and optimization process, r.js will parse main.js and following all of its defined dependencies. These dependencies will then be concatenated and minified into the "main-built.js" file.

When I run the above code, I get the following terminal output:

Tracing dependencies for: main
Uglifying file: /testing/jquery/requirejs-1.0/optimizer/app/main-built.js


This produces main-built.js. And, when I open up main-built.js, I get the following "stuff" (I've added line breaks to make the wrapping work):

main-built.js (Our RequireJS-Optimized Application File)

(function(){var a=["Msxml2.XMLHTTP","Microsoft.XMLHTTP","Msxml2.
(\s)*\?>/im,c=/< body[^>]*>\s*([\s\S]+)\s*<\/body>/im,d=typeof
i,j,k;return typeof window!="undefined"&&window.navigator&&
window.document?j=function(a,b){var c=i.createXhr();"GET"
c.responseText)},c.send(null)}:typeof process!="undefined"&&
Packages!="undefined"&&(j=function(a,b){var c="utf-8",d=new,e=java.lang.System.getProperty("line.
try{g=new java.lang.StringBuffer,h=f.readLine(),h&&h.length()
strip:function(a){if(a){a=a.replace(b,"");var d=a.match(c);
d&&(a=d[1])}else a="";return a},jsEscape:function(a){return
.replace(/[\r]/g,"\\r")},createXhr:function(){var b,c,d;if
(typeof XMLHttpRequest!="undefined")return new XMLHttpRequest;
for(c=0;c<3;c++){d=a[c];try{b=new ActiveXObject(d)}catch(e)
{}if(b){a=[d];break}}if(!b)throw new Error("createXhr():
XMLHttpRequest not available");return b},get:j,parseName:
function(a){var b=!1,c=a.indexOf("."),d=a.substring(0,c),
e=a.substring(c+1,a.length);return c=e.indexOf("!"),c!==-1&&
?\/\/([^\/\\]+)/,useXhr:function(a,b,c,d){var e=i.xdRegExp.
exec(a),f,g,h;return e?(f=e[2],g=e[3],g=g.split(":"),h=g[1]
e.inlineText&&(h[a]=c),d(c)},load:function(a,b,c,h){var j=i
:function(a,b,c,d){if(b in h){var e=i.jsEscape(h[b]);c.
asModule(a+"!"+b,"define(function () { return '"+e+"';});\n")}}
,writeFile:function(a,b,c,d,e){var f=i.parseName(b),g=f.
i.load(g,c,function(b){var c=function(a){return d(h,a)};c.
asModule=function(a,b){return d.asModule(a,h,b)},i.write(
{return"This is text from the data.txt file."}),define(
"mod-1",["./subsequent"],function(a){return"This is mod-1
return"This is mod-2 ["+a+"]"}),require(["text!./data.txt",

Pretty crazy stuff! If I go into my main HTML file, however, and change the "data-main" attribute from "main" to "main-built", my page runs properly! In fact, I get the exact same console output:

Data: This is text from the data.txt file.
Mod-1: This is mod-1 [SUB-MODULE]
Mod-2: This is mod-2 [SUB-MODULE]

This time, however, when you look at the Firebug Network activity tab, you see the following:

HTTP requests needed after the RequireJS build and optimization process.

As you can see, this significantly reduced the number of HTTP requests that needed to be made. One thing that I don't quite understand, however, is why "data.txt" needed to be loaded as an additional HTTP request. If you can look through the minified code produced by the RequireJS build tool, you can see that the content of the "data.txt" file has been inlined with the code. As such, I am not sure why this is showing up as an external dependency.

NOTE: After a little bit of experimenting, I discovered that if I removed the "./" from "./data.txt" dependency definition, the subsequent HTTP request would no longer be necessary. This must have something to do with the way the resource name is being normalized. The "./" must throw off the normalization during the build process?

This stuff is pretty awesome! And, really painless to use (as long as you have node.js installed on your development machine). What's super nice about it is that I can continue to use all the modularity and code separation during development; then, for production, I can quickly and efficiently create a much more optimized JavaScript application file. Awesome pants!

Want to use code from this post? Check out the license.

Reader Comments


Wow, that was quick follow-up article, Ben :)

I didn't have a lot of code, so I put everything in one file right away and used curl ( instead of require.js, because it is lighter, as it provides less functionality.

There are also two mini-require implementations I know of. They are part of ACE ( and Dryice ( They didn't work for me out of the box like require.js and curl did though.

I think for a bigger project I'd use require.js and the optimizer. So thanks for posting this.



I've played a bit with LABjs, but only for the asynchronous loading. As far as dependency management, RequireJS is the only thing I've used. But, what's so nice about it so far is that, as you say, it just works out of the box :) I'm kind of jazzed up about this stuff. I want to try and code something a bit more complex to really wrap my head around it.


@Ben I believe the issue with ./data.txt is due to a bug resolving relative resources that are in the baseUrl directory. It will be resolved in the requirejs 1.0.2 release that should be out within a week. The workaround, since the modules are already at the baseUrl level -- which is the top of the module space -- is to just leave out the './', as you did.


@Ben: Yes, require.js worked like a charm right away. Thanks James :) Curl was also pretty easy to integrate (one difference is that the method is called "curl" instead" of "require").
Keep us posted on your findings :)



Ah, good to know re: 1.0.2. I think I'll probably just stop using "./" in general. It's a hold over from how I tend to write HREF values. But those point to file paths; I'd like to start thinking about this stuff more like "class paths" which don't have "./" constructs :)


@Ben: relative IDs are great when you have a set of related modules that are all in a particular directory, but yes, for things that are at the "top" of the module space, it does not buy much.

All that said, 1.0.2 was pushed out so it should work now however you do it.


I've been using RequireJS with Appcelerator's Titanium and it's giving me great results. It's making loading all the required JS files much easier and much more modular



this is great explanation. Finally i found a reason to completely fall in love with requireJS. Is there any tutorial of converting this optimzed code to offline web application?


Excellent post, thanks for stripping it down and walking through it. Just what I was looking for to get started.

Quick questions, what process are you using to switch between the two files for dev and production. For example: I deploy to Heroku, so it'd just a git push of the latest committed code, so I need to be able to swap from main.js to main-built.js. Any tips? Should I be using a grunt task that Heroku runs before starting my node app? I don't want to have to change it manually, commit it, push to production and then change it back for development.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel