The module system and its patterns
As we said, modules are the bricks for structuring non-trivial applications and the main mechanism to enforce information hiding by keeping private all the functions and variables that are not explicitly marked to be exported.
Before getting into the specifics of CommonJS, let's discuss a generic pattern that helps with information hiding and that we will be using for building a simple module system, which is the revealing module pattern.
The revealing module pattern
One of the bigger problems with JavaScript in the browser is the lack of namespacing. Every script runs in the global scope; therefore, internal application code or third-party dependencies can pollute the scope while exposing their own pieces of functionality. This can be extremely harmful. Imagine, for instance, that a third-party library instantiates a global variable called utils
. If any other library, or the application code itself, accidentally overrides or alters utils
, the code that relies on it will likely crash in some unpredictable way. Unpredictable side effects can also happen if other libraries or the application code accidentally invoke a function of another library meant for internal use only.
In short, relying on the global scope is a very risky business, especially as your application grows and you have to rely more and more on functionality implemented by other individuals.
A popular technique to solve this class of problems is called the revealing module pattern, and it looks like this:
const myModule = (() => {
const privateFoo = () => {}
const privateBar = []
const exported = {
publicFoo: () => {},
publicBar: () => {}
}
return exported
})() // once the parenthesis here are parsed, the function
// will be invoked
console.log(myModule)
console.log(myModule.privateFoo, myModule.privateBar)
This pattern leverages a self-invoking function. This type of function is sometimes also referred to as Immediately Invoked Function Expression (IIFE) and it is used to create a private scope, exporting only the parts that are meant to be public.
In JavaScript, variables created inside a function are not accessible from the outer scope (outside the function). Functions can use the return
statement to selectively propagate information to the outer scope.
This pattern is essentially exploiting these properties to keep the private information hidden and export only a public-facing API.
In the preceding code, the myModule
 variable contains only the exported API, while the rest of the module content is practically inaccessible from outside.
The log
statement is going to print something like this:
{ publicFoo: [Function: publicFoo],
publicBar: [Function: publicBar] }
undefined undefined
This demonstrates that only the exported
properties are directly accessible from myModule
.
As we will see in a moment, the idea behind this pattern is used as a base for the CommonJS module system.