Skip to main content
🧙‍♂️ Kris Kowal 🐘 🐙 🧛 📌 📬 Book Store

CommonJS Packaging

This is the third part of a chronicle of the CommonJS modules debate. The first part has the timeline and the method, and the second the module itself. This one is about packages: how a module names a module in another package, how the list arrived at a package descriptor, why it stopped short of versions and installation, what Narwhal and npm did instead, and how the engine overlays we built in Narwhal in 2009 came back, ten years later, as the conditions in npm's exports and imports.

As before, each design point is an IBIS outline: the issue, the positions, the arguments for and against, and the resolution, every node linked to the message that carries it.

Before there was a package.json #

Versions came up on the list's fourth day, and the first answer was no. Peter Michaux argued that versioning "should not be supported in the standard," with the case that would haunt every package manager since: A requires B and C, B requires version 2 of D, C requires version 3, and if D "truly needs to be a singleton, then all hell breaks loose and you don't even know it." Wes Garland agreed: "Versioning is also kind of anti-JS," and "We should be doing "capability testing" whenever possible."

Peter also opened the first package thread, a "target for rock throwing", which already assumed that a library might come in one package per engine: print_spidermonkey, print_rhino, greet_all.

Meanwhile Tom Robinson and I were building Narwhal, which began as the platform under Tom's Jack. In April 2009 I gave it a package manager, with a package.json in each package (280north/narwhal bf774e4), and changed how it found engine-specific modules to a "platform load path overlay instead of platform id replacement" (b89a969). Narwhal's answer to "where are a package's modules" was simple: lay every installed package's lib directory onto one search path. That package.json was the first, as far as I know, and I take the credit for it; the dependencies that lasted, devDependencies and the node_modules tree are npm's (part 4).

How a module names a module in another package #

Issue: who owns top-level identifiers, and how does a module reach a module in another package?

So a top-level identifier, which I had meant for the shared standard library, came to mean the name of a dependency, resolved from the requiring module's own directory upward:

/usr/lib/node_modules/foo/node_modules/bar.js does require('baz'):
  /usr/lib/node_modules/foo/node_modules/baz
  /usr/lib/node_modules/baz

That example is Node's own, from the documentation that landed with the change. Mark S. Miller had already said what he thought of a namespace shared by everything on a machine: "A global namespace is a bug. Period."

node_modules made the namespace per package, which answered him, but kept the names the dependencies chose for themselves, which mappings would have let the dependent choose. Of the two, node_modules was the one that could be built without anyone's agreement: a package manager and a loader, and no new metadata. In How to design a module system I argue that a dependency should not dictate the name its dependents use for it, which is what mappings were for. npm got the locality right and left the naming to the registry, though not without recourse. Since npm 6.9.0 (npm/cli b7b54f2d1, March 2019), a little-known workaround lets a dependent rename a package in its own dependencies with the npm: protocol:

"dependencies": {
  "q1": "npm:q@^1",
  "q2": "npm:q@^2"
}

Then require("q1") and require("q2") load two major versions of q side by side. The name is the author's unless the dependent chooses another: simple is easy and complex is possible, and the lightest form serves the most common case. That is good form, though it could be more obvious that the workaround is there: npm's documentation for npm install did not mention aliases until that November (b150eaeff).

The package descriptor #

The descriptor itself was the list's most efficient piece of work. Michael O'Brien posted Packages 0.1 on December 16, 2009, mined from what Narwhal, Ejscript and others already used, and announced it ratified as Packages/1.0 on January 12, 2010: "With 213 messages starting on Dec 16 and finishing on Jan 12, that has been a great effort and good quick progress."

Issue: what does package.json specify, and does CommonJS specify layout, installation and registries too?

The one field everyone uses today came late and from npm. Isaac asked in January 2010: "Can we add a "main" module to the packages.json spec?" I objected only to the name, since in Narwhal main meant the module to run when a package is executed, and Ash Berlin saw the point at once: it "saves having to do require('package/package')." Isaac also wanted the rest of a package hidden: "If someone is doing require("npm/utils/registry/adduser") then they're Doing It Wrong, and it will break a lot. Hiding the internal modules is a desirable feature for many packages" (Isaac). That wish waited until Node's exports field, in 2019.

Versions #

Issue: what does a dependency's version mean?

Narwhal answered the same questions differently. A Narwhal package listed its dependencies as an array of names, a format I asked Packages/1.0 to grandfather in. Its loader read each package's version, but resolved dependencies by name alone: one package per name in the catalog, any package whose named dependencies were missing thrown away, and every package's modules on one search path (verify and analyze in narwhal-lib's packages.js at 8853ba0). That gave a whole system one version of everything, which is Peter Michaux's singleton guarantee, and the diamond problem he described, with no way out but to rename a package.

I would not call Narwhal's way better. npm's nested node_modules let two versions of a package live side by side, which the module system made safe, as Isaac said. What I miss from the CommonJS text is its reading of a version: the least version a package was tested with. That is the reading of minimal version selection, which I praise in How to design a module system, and it is not the reading npm chose.

Overlays, and the conditions that came back #

Narwhal ran on several engines, and some modules had to differ by engine. Its answer was the overlay. A package could carry an engines/<engine>/lib directory beside its lib, and the loader put those directories first, for each engine the system ran, from most to least specific. Narwhal 0.2's own readline package was laid out this way:

packages/readline/package.json
packages/readline/engines/rhino/lib/readline.js
packages/readline/engines/default/lib/readline.js

On Rhino, system.engines was ["rhino", "default"], so require("readline") found the Rhino module first (lib/packages.js and engines/rhino/bootstrap.js at v0.2).

Issue: how does one package carry different modules, or different metadata, for different engines?

This is Kris Zyp's pattern, from June 2010, for choosing a module by engine within a package:

"overlay": {
    "node": {
        "mappings": {
            "store": "./engines/node-store"
        }
    },
    "rhino": {
        "mappings": {
            "store": "./engines/rhino-store"
        }
    }
}

And this is the shape Node gave the same idea, a decade later. Node introduced the "exports" field in 12.7.0, conditional exports in 13.2.0 and 12.16.0 (unflagged in 13.7.0 and 12.17.0), and the "imports" field, for a package's private identifiers, in 14.6.0 and 12.19.0 (doc/api/packages.md). The first commits were Jan Krems's "implement "exports" proposal for CommonJS" (dcb6929183, July 2019) and Guy Bedford's "conditional exports with flagged conditions" (2367474db4, October 2019). In a package today:

{
  "name": "store",
  "exports": {
    ".": {
      "node": "./engines/node-store.js",
      "default": "./engines/browser-store.js"
    }
  },
  "imports": {
    "#store": {
      "node": "./engines/node-store.js",
      "default": "./engines/browser-store.js"
    }
  }
}

The differences are the lessons of the overlay argument. The conditions are matched in the order the author writes them, with a "default" at the end, which is Narwhal's list of engines from most to least specific, written into the package instead of the platform. They remap identifiers, as Kris Zyp's overlay mappings did, rather than replacing a package's metadata. And the condition names are not only engines: "import" and "require" choose by how the package is loaded, and the community has added others, which is the more general predicate I asked for in March 2010. "exports" also hides every module it does not name, which is what Isaac asked for in July 2010.

What CommonJS left to npm #

The list specified the module, and npm, with Node, specified the package.

From the module side, npm inherited exactly what the shows of hands decided: a scope per module, require, the identity of the exports object, ./ for relative identifiers, and nothing like include. From the package side, it inherited a descriptor whose required fields it mostly ignored, the main its own author proposed, a version field whose meaning the list never settled, and an overlay that Isaac agreed to read and called a bad smell three months later. Everything the list argued and could not agree on, npm decided by shipping: where packages live, how a top-level identifier finds one, what a range means, and, ten years on, how a package chooses a module by condition.

I am content with how the module half came out. The package half I would still do differently, as I note in the last article of On Modules, but the record shows that the list had most of the right questions in front of it, and some of the right answers, in 2010.


The JavaScript Modules Chronicle:

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