Software Design by Example 13: Module Loader


The previous chapter showed how to use eval to load code dynamically. This chapter uses some of those ideas to build a small version of JavaScript’s require function. This function takes the name of a source file as an argument and returns whatever that file exports (typically, a bunch of constants and functions). The key requirement for such a function is to avoid accidentally overwriting things: if we just eval some code and it happens to assign to a variable called x, anything called x already in our program might be overwritten.

The real focus of the chapter is therefore how to encapsulate what we’re loading, i.e., how closures work and how to use them to implement namespaces. Our approach is based on the excellent tutorial in Casciaro and Mammino’s book Node.js Design Patterns, which contains a lot of other useful information as well.

Implementing modules with IIFEs (part 1)
Figure 13.2: Using IIFEs to encapsulate modules and get their exports (part 1).
Implementing modules with IIFEs (part 2)
Figure 13.3: Using IIFEs to encapsulate modules and get their exports (part 2).

Terms defined: absolute path, alias, circular dependency, closure, directed graph, encapsulate, immediately-invoked function expression (IIFE), inner function, Least Recently Used cache, namespace, plugin architecture.