Skip to main content
πŸ§™β€β™‚οΈ Kris Kowal 🐘 πŸ™ πŸ§› πŸ“Œ πŸ“¬ Book Store

CommonJS Feature Discussions

This is the second part of a chronicle of the CommonJS modules debate, reconstructed from the list archive. The first part has the timeline and the method. This one is about the module itself: whether it gets its own scope, whether it may import everything another module exports, what require returns, how a module exports, and the two kinds of module identifier.

Each design point is drawn as an IBIS outline: the issue, the positions with who took them, the arguments for and against, and the resolution. Every node links to the message that carries it.

A scope of one's own #

Issue: does a module's code run in the global scope or in a scope of its own?

Most of the rest of this article is about the consequences of that choice. Once a module had a scope of its own, every other question was about what could cross into it.

Wildcard imports: the case against include #

My own 2008 module standard had an include. It took everything another module exported and bound it in the importing module's scope, the way Python's from module import * does. I could not imagine a community adopting a module system without one. The list did, in about three days.

Hannes's Helma NG had all three forms:

import('helma.file');            // helma.file.File
include('helma.file');           // File, and every other export
var file = require('helma.file'); // file.File, named by the importer

Issue: should the standard let a module bring all of another module's exports into its own scope?

The argument that settled it for me was in that same message to Tom: "there's nothing like actually having sovereignty in your own name spaced file to give you peace of mind, especially when you're sharing your name space with unknown, potentially malicious peers."

Implementations kept include for a while as an extension. v8cgi's filled the global scope with a module's exports. Node had one too, until Ryan Dahl removed it on October 5, 2009, writing that "include() should not be used by libraries because it will pollute the global namespace. To discourage this behavior and bring Node more in-line with the current CommonJS module system, include() is removed" (nodejs/node 8185e1fd25).

Three days later the question came back in a thread about QUnit, and I explained why I had changed sides:

This opens an old wound; it was amply discussed in January and I was originally on the pro-include side of the debate with a large body of code using it already. I agree that it's convenient; but I've been convinced it's a bad idea for standardization because, we can frankly live without it [...], and eventually we'll be able to use destructuring like var {F} = r("f") which is about as convenient and safe. It's also become quite obvious to me that while going from require to include would be easy, it is extraordinarily difficult to do the reverse since information about which free variables are in use and where they are from is effectively lost. If that's difficult for the author of a body of code, it's impossible for someone trying to learn how your code works.

Kevin Dangoor thought include was "nice as an extension" for the rare cases where he would use from something import * in Python. Hannes still used it "all the time," for modules whose exported names stand on their own, like assertEquals. Neither asked for it back in the standard.

I would put the asymmetry first now. A wildcard import is a one-way door: the author saves a line, and every later reader, and every later version of the imported module, pays for it. That is the hazard I describe in How to design a module system, and CommonJS closed that door by leaving include out.

What require returns #

Issue: is the object require returns the module's own exports, or something standing in front of it?

I called for hands on five options, A through E, and Peter Michaux added F:

E is why two modules can require each other. Ihab told the history in December: "Historically, Kris Kowal's and my first thought about modules was that they would construct and return the exported object. We quickly realized that this caused problems when modules require() one another in a cycle." The loader passes the module an exports object before running it, and in a cycle, require hands back that object half built.

Replacing the exports object #

Which brings us to the first decision the list could not make.

Issue: may a module export a single value, a constructor say, in place of the object it was given?

So the most common line in the npm ecosystem today is one the CommonJS specification never allowed:

module.exports = function Foo() {};

The list was right that replacing the exports object breaks cycles, and the ecosystem was right that people wanted to export one thing. Node's choice won by shipping, not by argument. Years later, ESM's export default was added for parity with module.exports, as the last part tells.

Two kinds of identifier #

The distinction between relative and so-called absolute identifiers is one I wrote down myself, "based on some feedback, and some personal preference (based on minimizing the amount of effort I'll have to do to port the scripts I've got)," as I admitted at the time.

On February 6, 2009, I wrote:

1.) A module identifier is a String of "terms" delimited by forward slashes.

2.) A term must be a camelCase identifier, ".", or "..".

3.) The extension of the file corresponding to a module identifier must be inferred by loaders.

4.) Module identifiers may be "relative" or "absolute". A module identifier is "relative" if the first term is "." or "..".

5.) Absolute identifiers are resolved off the conceptual name space root. A loader may check multiple roots in a consistent order, like a PATH.

6.) Relative identifiers are resolved relative to the file in which "require" is called.

Issue: which identifiers are relative, and what are the others relative to?

A second issue followed from the first: relative to the requiring module's identifier, or to its file? I argued for the identifier, because "it must be possible for module loaders to be created for systems that are not on a file system," and because path-relative resolution could give two files the same identifier. Wes argued for the file, since a module asking for its sibling should get its sibling. The specification says the identifier. In practice, with one file per module and one directory per package, the two readings rarely differ.

What the specification did not settle was who owns the top-level names. I thought I knew. When Ben Newman asked how to graft a foreign library under a prefix of his own, when its modules used top-level identifiers among themselves, I answered: "This is a big problem with absolute identifiers. As a matter of practice, we should only use absolute identifiers for CommonJS modules, but that is not yet really feasible." And in April 2010, at greater length, "The top-level module identifier name space belongs to CommonJS."

That is not how it went. In Node and npm, a top-level identifier came to name a package, looked up from the requiring module's own node_modules and its ancestors'. How that happened, and what the list proposed instead, is in the third part.

Module metadata and the 1.x revisions #

The other decisions about the module were smaller, and the shows of hands settled them.

In June 2009 I called for hands on Modules/Meta, which gave require a main and a paths, and gave every module a module object with an id, such that require(module.id) returns the same exports. The wiki marks it "ratified and integrated in Modules/1.1." Node renamed its module.name to module.id "for commonjs compat" (f683867b98) the same day it went "almost CommonJS compatible."

Modules/1.1.1, in March 2010, fixed what require.main is when there is no main module. The draft said an empty object; Hannes wanted undefined, so that 1.1.1 would be a strict extension of 1.0, and he got it. The text that resulted is the one that makes a familiar idiom work: require.main "must either be undefined or be identical to the "module" object in the context of one loaded module."

if (require.main === module) {
    main();
}

The page lists the hands: Mikeal, Gozala, Zach Carter, Hannesw, Tobie, OndΕ™ej Ε½Γ‘ra, Charles Jolley, KrisKowal, Chris Zumbrunn, Gmsox.

require.paths did not last. Changing it at run time "was never more than a stop-gap," as I put it in 2011, and "the require.paths.push(".") pattern needs to go away." Node removed it that July (7f0047c2d5).

Transport, and the split with AMD #

The browser was the list's longest argument, and it ended in a split rather than a decision.

Issue: what wrapper does a browser loader use to receive CommonJS modules through script tags?

Through all of it I held that require takes only a string literal, so that a loader in a browser can find a module's dependencies before running it. I meant CommonJS primarily for the web, and the last part follows that thread to ESM.


The JavaScript Modules Chronicle:

  1. The CommonJS Dialectic
  2. CommonJS Feature Discussions
  3. CommonJS Packaging
  4. CommonJS Retrospective