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?
- One shared tree: lay each package's
libonto the search path. This was Narwhal's way. Its loader sorted the installed packages by their dependencies and put each one'slib(and engine directories, below) on a single path (lib/packages.jsatv0.2, December 2009). Irakli Gozalishvili proposed it as the standard, "as it's a way currently narwhal works."- Against: "requiring everyone to install packages at the top level seems unreasonable and very prone to conflicts" (Kris Zyp). Irakli himself came to think that overlapping identifiers brought "overlap issues with 0 real benefit".
- A second argument to
require:require(id, package), where the package is a short name the requiring package declares for itself. Ihab Awad's Packages proposal, put to a call for votes in September 2009; later Ihab and Christoph Dorn'srequire2.- For: as I put it, it "guarantees safe composition by deferring the name space problem to the URL name space. It is furthermore securable and gives package authors the right to define their own names without interfering with each other."
- Against: "it's a little goofy that the first argument to require changes based on the presence or absense of a second argument" (Dean Landolt).
- A URL scheme in the identifier:
package://name/module, which I suggested, or a leading/for the system's packages, which Ryan Dahl suggested on IRC, or a separaterequirePackage(). mappingsinpackage.json: each package maps the top-level identifiers its modules use onto the packages it depends on. Kris Zyp proposed it in April 2010: "packages themselves should get to decide how they will reference modules from other packages." I wrote a revision, Packages/Mappings/C.- Against: "It's not far off from what I've implemented with npm, but it seems a whole lot more complicated without really adding much value" (Isaac Schlueter), and later, "it's 4 solutions glommed onto a single object" (Isaac).
- Let the package manager arrange folders so that the engine's plain
requirefinds each package's own dependencies. Isaac, describing npm in May 2010: each installed package got adependenciesfolder of links to the versions it asked for, unshifted ontorequire.pathswhen it loaded.- For: "There is no manifest. There is no runtime calculation or analysis whatsoever. npm just puts the links in place, and lets node's extremely un-clever resolution logic do what it does. The folder structure is the database." And: "a package manager should not be involved at run time."
- For: "I'm not so convinced that a single namespace is a big problem, however. Couldn't you just name your jack fork something different? I mean, it IS different, so why not NAME it differently?" (Isaac).
- Resolution. The last position, and not on the list.
On October 13, 2010, Isaac committed "require looks in node_modules
folders" to Node: "for modules starting with the __dirname and moving up.
This makes it much easier to localize dependencies to a particular
program" (
nodejs/nodeb0adaff67e). Ryan Dahl reverted it a month later (a375c6ae9a), and it landed again with documentation in February 2011 (46513483cd), in Node 0.4.0. npm 1.0 installed packages locally, intonode_modules(npm/cliv1.0.1, April 30, 2011), and Node removedrequire.pathsthat July (7f0047c2d5). - Later. I would have named the directory
deps, since tools other than Node were always going to read it, and I would still have Node readpackage.jsonat run time and refuse a dependency it does not declare, as mappings would have (part 4).
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?
- A full descriptor, with many required fields, and package names that
are opaque and global: "The name in the package.json should be THE name"
(Michael O'Brien).
- For: tools and portals need the metadata, and "Package descriptors should not have physical locations of other packages in them. That becomes a nightmare to maintain" (Michael).
- Against: "required properties should be limited to information installers, managers and package registries MUST have" (Mikeal Rogers), and "npm doesn't require anything more than a name and version" (Isaac).
- Separate the catalog from the descriptor, and do it in phases, which I proposed: first system packages with short names, then a catalog schema, then packages that name their dependencies by catalog, hash and signature.
- Split structure, loading and discovery apart, and standardize dependencies by URL. Charles Jolley, in "Towards a Simplified Packages Spec", June 2010: package managers had multiplied, "all of them claim to support CommonJS packages and yet they are all distinct and incompatible."
- Resolution. Packages/1.1, which Mikeal shepherded to a
last call
in July 2010, made most fields optional, required
mainorlib, and explicitly left mappings out: "we aren't taking on mappings or any other "controversial" changes and/or additions in this version." Catalogs, registries and installation were never specified.
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?
- Nothing, in the standard: leave versions out (Peter Michaux, February 2009, above).
- Semantic Versioning, with a bare version as the least compatible
one.
Tom Robinson
suggested semver
the day Packages 0.1 appeared.
I
read
"1.2" as "greater or equal to "1.2.0" but less than "2"," and hoped that
"semantic versioning should alleviate the need for complex version
predicates."
Then I
called for hands
on the upper bound:
- Zero: a dependency on X.Y.Z means through X + 1, exclusive. For: "except for errors, semantic versioning provides strong guarantees." Against: if a maintainer falls asleep and old versions are culled, "it is possible that a package will become unusable." Ash Berlin chose Zero, and so did I, adding that "any catalog validator I write will ban it and send hate mail" if anyone smuggled in Infinity (me).
- Infinity: no upper bound unless stated. For: "packages may go unmaintained indefinitely without causing their dependees to ever become unusable," and an Infinity system can always state an upper bound, while a Zero system cannot remove one (Michael O'Brien, who voted Infinity).
- Exact versions, and explicit ranges when you want a range.
Isaac, for npm, in March 2010:
"If you tell npm that you want version 1.2.3, then that's the version
you're getting. Not 1.3.0, not 1.2.4, nothing else. if you tell it you
want something at least version 1.2.3 but less than 1.3.0, then you can
tell it that, and it'll make sure that's what you get. You can tell it
that by writing ">=1.2.3 <1.3.0" in the dependency field"
(Isaac).
- For: "it is my firm belief that no one understands semver, or implements it 100% according to the spec. No one" (Isaac).
- For: "It's so easy to have multiple versions of the same package when you have securable modules, so why not give packages exactly what they ask for, and avoid dependency hell" (same message).
- Resolution, in three places.
Packages/1.0 took the Zero reading: each dependency names "the lowest
compatible MAJOR[.MINOR[.PATCH]] dependency versions (only one per MAJOR
version) with which the package has been tested and is assured to work."
When Mikeal
laid out
strict semver, loose semver and explicit ranges for 1.1, the list settled
none of them, and Packages/1.1 made
dependenciesoptional. npm's ranges became the practice. - Later. I feel vindicated for keeping versions in a manifest outside the module, though in March 2009 I had left room for them in module identifiers (part 4).
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?
-
Engine directories overlaid on the search path, as Narwhal did. I had described the idea in February 2009, platform-specific modules in a package "laid over the module root, supplanting anything that was there before" (me).
- Against: I did not think it belonged in the standard: "overlay-style packages are a Narwhal-specific feature, necessary for multi-engine bootstrapping, that does not need to be a CommonJS standard, although there's certainly also no need to preclude it" (me).
- Against: it costs a lookup per directory, which is expensive in a browser (Kris Zyp).
-
An
overlayobject inpackage.json, keyed by engine, whose properties replace the top-level ones. Mikeal proposed a"specific"key in March 2010, with a shallow replacement of each property; Isaac preferred"env", and wrote "I'm going to add support for this in npm right now, that's how hard I love it"; and I wrote, in full, "I see your "env" and raise you "overlay"." Isaac: "Sure, overlay works, too."- For: "Best of all, we don't have to agree, we just have to agree how we're going to go about disagreeing, which is a MUCH easier thing to get resolution on" (Isaac).
- Against: I worried that engine names were too narrow a predicate: "this implies predicated overlays, which could be a much more general idea, and raise more questions than they answer. [...] Would it be better to provide a more general form of predicate than just engine/vendor name matching?" (me).
- Against: by June, Isaac called it "sort of a bad smell that we should try to get away from, a mark of an unfinished spec" (Isaac).
-
Overlays that remap module identifiers within the package. Kris Zyp was already using them in Nodules (2010-06-01), and wanted predicates richer than engine names:
"node<0.1.90","es<5","!event-loop", or"debug"(2010-05-10). -
Feature detection instead of engine names. Christoph Dorn thought "feature detection would be better than engine overlays for package.json," and in 2011 I sketched a mapping that would link either a global or a package,
"detect": "JSON"(me). -
Resolution. The Packages/1.1 draft added
overlay, "Object hash of identifiers for conditional replacements of top level properties," with this example:"overlay": { "node" : {"dependencies":[...]}, "npm" : {"scripts":{"install":"./npm-install.sh"} }No registry of engine names was ever agreed, and no conditional module resolution was specified.
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:
- The CommonJS Dialectic
- CommonJS Feature Discussions
- CommonJS Packaging
- CommonJS Retrospective
- Previous: Interoperable JavaScript Modules
- Next: Map of Middle-earth