curl.js: Incredible AMD Loader

By on  

Today there are dozens of AMD JavaScript loaders available, the most popular being RequireJS. There are also lesser known JavaScript loaders like YepNope, $script.js, LABjs, and Dojo's new native loader. My favorite JavaScript loader, however, is John Hann (unscriptable)'s curl. While allowing for maximum configuration and reliable loading, curl also allows for loading of simple JavaScript files as well as CSS files. Let me show you how to use it!

Super Quick AMD Primer

If you aren't familiar with AMD structure, I'm going to give you the most oversimplified explanation you'll ever hear. AMD is a system by which you define and require modules asynchronously. A define returns one or zero objects. The first argument of both define and require is (usually) an array of dependencies. The second argument is a function; the define returns the result, the require executes a basic callback:

// "define" a module
define(["namespace/dependencyA", "namespace/dependencyB"], function(depA, depB) {
	// Whole bunch of processing
	// Return what this module defines
	return function() {
		// Or an object, or whatever

// "require" to use modules:
require(["namespace/dependencyC"], function(depC) {
	// depC can be used in here only
	// Yay for modularity!

The slashes in the dependency array items represent paths to module JavaScript files. Once dependencies are loaded, the action is allowed to begin.

As I said, this is a very simple, vanilla example; there are exceptions to every rule, so don't bother pointing out what-ifs.

Configuring Module Loading with curl

And of course I start out with a few of the exceptions to the rule. Instead of a require function, curl.js defines curl in its place. Additionally, curl.js allows for an object literal as a first parameter, allowing for configuration of loaded modules:

		baseUrl: "/path/to/js",
		pluginPath: "curl/src/curl/plugin"
	["namespace/depC", "namespace/otherDep"],
	function(depC, otherDep) {
		// Do stuff

This configuration allows you to provide plugin paths, modules paths, and more.

Basic define and require with curl.js

Basic usage of curl.js is as you would expect from a JavaScript loader; dependency array as the first argument, callback with the second:

define(["namespace/depA", "namespace/depB"], function(depA, depB) {
	// Do something with the dependencies
	// Pump out a return obj
	return myFinalObject;

With a module defined, the same syntax requires and works with the dependencies:

curl(["namespace/depC"], function(depC) {
	// Do some stuff!

This is the same syntax you will have used with any JS loader, with the obvious exception of require being replaced by curl.

curl.js with next

The next method allows for chaining of module loading:

	.next(["dep1", "dep2", "dep3"], function (dep1, dep2, dep3) {
		// Execute regardless of domReady status
		function () {
		// do something after the dom is ready
		function (ex) {
		// show an error to the user

This syntax may suit your fancy more than others.

curl.js with Deferred Syntax

If you work with the Dojo Toolkit, or more recently with jQuery, Deferreds are becoming more prevalent and incredibly useful; curl.js provides you the ability to write your loader JavaScript in the same fashion:

	function(depA) { // success callback
	function(depB) { // errback

The deferred format and ability to pass the result of an XHR pool can be very powerful.

Loading Non-AMD JavaScript Files

Sometimes you need to load JavaScript files that aren't in AMD format, like loading MooTools or jQuery from CDN. curl.js makes that easy:

).next(["namespace/MooModule"], function() {
	// We loaded Moo first, then once loaded, loaded a dependency that requires MooTools
	// At this point, both are loaded and we can work with them!

All you need to do add the js! prefix to the dependency string and you're set; your callback will be fire when the basic JavaScript file is loaded. Note that you can mix AMD modules with basic JavaScript files:

).next(["namespace/MooModule"], function() {
	// We loaded Moo first, then once loaded, loaded a dependency that requires MooTools
	// At this point, both are loaded and we can work with them!

Loading CSS Files

Of course one of the strengths of AMD is modularity, so why not load your stylesheets with your scripts?

	function(MyWidget) {
		// Do something with MyWidget
		// The CSS reference isn't in the signature because we don't care about it;
		// we just care that it is now in the page

LINK tags don't provide an onLoad event in all browsers, but curl.js' shim provides a reliable method of detecting stylesheet load. Since stylesheets are a large part of UI-driven, JavaScript-powered widgets, creating modules with stylesheet dependencies is becoming much more abundant.

More curl Plugins

curl is much more than just a basic JS loader. I've already mentioned the JS and CSS plugins above, but curl has a few more. curl features a domReady plugin, as well as a text plugin and an internationalization plugin:

		"i18n!stuff/nls/strings", // Load string content for the user's namespace
		"text!myWidget/resources/template.html", // Loads a file as text,
		"domReady!" // Don't fire the callback until the DOM is ready
	function(nlsStringObject, template) { // Callback
		// Do something now that we have the NLS object, template, and domContentLoaded has fired

These plugins are quick and easy enhancers to existing functionality!

curl is an absolute beast of a JavaScript loader. Beyond simple AMD loading, curl is fit with numerous configuration options, plugins, and multiple syntax structures to all the developer to code the way they want. This blog uses curl.js to asynchronously load JavaScript modules and stylesheets, manage domReady, and more; the best endorsement I can give!

Track.js Error Reporting

Recent Features

  • CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • Create a Dynamic Flickr Image Search with the Dojo Toolkit

    The Dojo Toolkit is a treasure chest of great JavaScript classes.  You can find basic JavaScript functionality classes for AJAX, node manipulation, animations, and the like within Dojo.  You can find elegant, functional UI widgets like DropDown Menus, tabbed interfaces, and form element replacements within...

  • Styling CSS Print Page Breaks

    It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my...


  1. Yo Dave! I am honored!

    I love this article (for several reason). :)

    One quick suggestion: when you’re looking at your code in the editor, it’s easy to tell what module you’re looking at by the file path. In code snippets, it’s harder to tell so I usually do put a comment at the top of the module declaration. Something like this:

    // module namespace/depC
    define(["namespace/depA", "namespace/depB"], function(depA, depB) {
      // Whole bunch of processing
      // Return what this module defines
      return function() {
        // Or an object, or whatever

    Just another quick note about “curl” vs. “require”:

    The main reason curl doesn’t declare a global `require` by default* is that at the global level, you’re not in a standard environment. The standard `require` — also called the “local require” — exposes functionality that depends on the context of the current module (i.e. what package it’s in or where is it located within the entire folder structure of the app). I’ve seen many n00bs (using other loaders) grab a reference to the global `require`, expect it to behave in a standard way, and spend hours trying to figure out why it doesn’t work.

    Even worse: I’ve seen devs take advantage of proprietary features on another loader’s `require` object and then curse when their code isn’t portable to other loaders.

    curl.js only exposes proprietary features on the global object, not on the local require. In summary: having the global object expose proprietary features and keeping the local require 100% compliant to the standards will help avoid common n00b errors and protect your code from becoming unportable. (The goal of the AMD standard is to allow us all to write portable, reusable code.)

    There are some environments that may need a global “require”. For instance, test harnesses for AMD plugins will likely need to test with several loaders. curl.js has a config option for this.

    curl({ apiName: "require" });

    For reference to readers, here’s how to explicitly grab curl.js’s standard `require` and global, proprietary API object (a.k.a. the “curl” object):

    // my/awesome/modyule:
    define(["require", "curl"], function (require) {
    	// using the standard, local require:
    	var foo = require("foo");
    	return {
    		sillyAbstractionOfFoo: function () { return foo; }
    // my/awesome/modyule
    define(["curl"], function (curl) {
    	return {
    		// using curl's proprietary API to return a promise
    		getAndWaitForAModyule: function (moduleId) {
    			return curl([moduleId]);

    curl.js makes it pretty explicit when you’re doing something non-standard and I like that.


    — John

  2. Alex

    This is by far my favorite article of this year so far.
    Useful and in time. Never though I’d say that to another man…

  3. Manu

    Hi David, fyi the development of i18n plugin for curl seems in progress. Actually the plugin does not work.

  4. Paul

    How does one use the module defined using define ? I just can’t find that information anywhere.
    e.g. a moduleC is defined like: In a file: moduleC.js

    // stuff
    return function(){

    I guess this must be defined in the file moduleC.js
    How would I use moduleC in another file or module? what happens if I define more than one module in a file and I want to give each module a different name ?
    Appreciate any help.

  5. I’d love to learn the basics of AMD/Curl and use with non AMD libraries.

  6. Moritz

    To keep this article up to date:
    The repro from unscriptable moved to cujoJS: https://github.com/cujojs/curl :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Sort git Branches by Date

    I'll be first person to admit I don't do as much git repository maintenance as I should.  I rarely delete branches which have been merged, so a git branch execution shows me a mile-long list of branches that likely aren't relevant.  The best way to find branches I've recently...

  • Best Tools and Resources for Web Professionals in 2015

    Looking for the right resources to help you satisfy the needs of your clients? On the lookout for the best tools to help you increase your revenue? Searching for the right software to help you improve your business? Well, then you’ve come to the right place....

  • JavaScript Polling

    Polling with JavaScript is one of those ugly but important functions within advanced front-end user experience and testing practices.  Sometimes there isn't the event you can hook into to signify that a given task is complete, so you need to get your hands dirty and simply poll for...

  • OSCON Portland:  Conference Giveaway and Discount!

    O'Reilly puts on the best web industry conferences in the world.  These conferences include Fluent Conference, Velocity Conference, and the upcoming OSCON in Portland, Oregon from July 20-24.  Open Source Convention (OSCON) is a conference that focuses specifically on open source developers and the tools and possibilities...

  • Prevent Chrome from Translating a Page

    A while back I shared my favorite Google Chrome extension:  Google Art Project.  I've enjoyed seeing beautiful art when I open a new tab -- it's brought genuine happiness to my day, however small that happiness may be.  About a week ago, however, the art presented had...