Aliasing modules in RequireJS

A trick I discovered for substituting compatible libraries.

Ever wanted to alias a module in RequireJS? I have.

When your JavaScript project’s dependencies support AMD, the Asynchronous Module Definition specification, it is wonderful! No shimming necessary.

But alas, your dependencies might use module names that differ from your own or others’. Is it "Backbone" or "backbone"?

Or, maybe you want to substitute compatible libraries in your project. For example, I often prefer Lodash to Underscore for it extensions and performance, but many Backbone.js plugins expect a module named "underscore". Maybe you’d like to opt for Zepto.js instead of jQuery for its lightness.

Here’s a trick I discovered for using the "map" configuration:

require.config({
  map: {
    "*": {
      underscore: "lodash",
    },
  },
});

Voila!