CommonJS Retrospective
The first three parts of this chronicle tell how the CommonJS list argued its way to modules and packages, from the list itself. This one is my own view, looking back. I wrote most of it in February 2025, answering a question on my ask-me-anything repository, and I have arranged it here to answer the other three parts: the method, the module, and the package. Where I can, I let my messages to the list from 2009 to 2011 say what I said at the time.
Beyond my wildest dreams #
I am generally pleased with how CommonJS played out.
Rather, it has succeeded far beyond my wildest dreams.
I knew that a Cambrian explosion of code sharing was what I was working to
make happen, and yet at no point did I take for granted that JavaScript
modules would be A Thingβ’.
When I designed CommonJS, very few people wanted modules, and many people
were sincerely upset that I was pushing for modules in their sculpture
gardens.
On the list, that resistance was brief.
The first part shows Peter Michaux opening
with the warning that modules had been "a complete spoiler" for Helma NG,
and Tom Robinson holding out for a global require.
Within two weeks every hand was up for Securable Modules.
My designs were not the first.
I was particularly inspired by Python and Dojo.
My 2007 post JavaScript Modules Revisited
measures my loader against Dojo's and asks for "the flexibility of Python's
import syntax."
In the list's second week I
translated
Python's from foo.bar import baz into var baz = require("foo/bar").baz,
as the first of a "sequence of transitional syntaxes" Ihab Awad and I had
proposed to TC39.
When someone asked why identifiers use slashes and not Python's dots, I
said
the choice was arbitrary, and that "../../foo" is "much more intuitive than
"...foo"."
I disliked Google Closure's Java namespaces, and
grouped
Closure with Dojo and YUI as systems that "do name-spacing with provide and
require."
I sought to make a module system that was not siloed to a framework and
would not be mired in the limitations of the time of its creation, like our
anemic debugging environments.
Node.js and npm #
I did not expect Node.js to exist, much less to become the reference
implementation for CommonJS.
Ryan Dahl
announced Node
on the list on May 31, 2009, four months in.
The next day I
tried
to build a Narwhal platform on it and could not, because Node's require
was asynchronous then and Narwhal's bootstrapping needed to read files
synchronously.
Node made require synchronous that August (nodejs/node 79f121044c).
Node and npm fleshed out details of packaging that I did not design, and
that have proven important and mostly good.
I do take credit for the first package.json.
Narwhal read one in every package from April 5, 2009 (280north/narwhal
bf774e4), and the list's first mention of the file is
mine,
on April 14, five months before Isaac Schlueter's first commit to npm.
The word dependencies was Narwhal's first as well: "We're using
"dependencies" in package.json presently on Narwhal, and "depends" in
Jake," I
wrote
in September 2009.
But Narwhal's was a list of names, and by May 2010 I was
telling
the list that Narwhal and npm used the word "for different purposes."
I do not take credit for the design of dependencies or
devDependencies: npm's object of names and ranges is the design that
lasted, and it is npm's.
Much less do I take credit for the node_modules tree or the application
of semantic versioning and conflict resolution.
Semantic versioning was on the list before npm applied it: Tom Robinson
suggested it
the day Packages 0.1 appeared, and Packages/1.0 read a version as the least
one a package was tested with.
What npm brought was the ranges, and the nested tree that let two versions
of a package live side by side, as
the third part tells.
I would have named it deps #
I would have named the node_modules directory deps.
It is obvious in both hindsight and foresight that the dependency graph
would be shared between Node.js and all manner of other tools, and baking
the product name into the API is almost always an error.
Not everyone foresaw it: when Mikeal Rogers asked in May 2011 whether the
list was still relevant, he
wrote
"i doubt node_modules will be adopted outside of node."
The coin-toss #
I was responsible for the coin-toss on whether to distinguish relative
module identifiers (like ./foo) from top-level ones (like fs).
On February 6, 2009, I
specified
module identifiers "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)."
Peter Michaux
proposed
the first half of the alternative that same day, plain names relative and a
leading slash absolute, and
the second part has the arguments on
both sides.
I have no idea whether I made the right call.
But I do feel vindicated for insisting on a distinction.
In 2009 a top-level identifier did not yet mean a foreign package.
It meant a name in a namespace shared by everything, and in April 2010 I
still held that
"the top-level module identifier name space belongs to CommonJS."
The distinction I insisted on was between relative and shared.
It became the distinction between package-local and foreign when Node and
npm made a top-level identifier the name of a dependency.
A sigil for packages did come up once, in 2011, when Wes Garland
suggested
require("@registeredNamespace/moduleIdentifier") to keep registered and
local packages apart.
I feel vindicated too for insisting on package versioning manifests that
live outside the module.
Before the packaging threads, in March 2009, I
wrote
that "we have also been careful not to preclude the possibility of
extending the module identifier name space to eventually include
versions."
But from the first packaging threads I wanted versions "optional in the
format, and enforced by managed package catalogs"
(September 2009),
and Narwhal's Tusk could
freeze
the installed versions into a freeze.json to reinstall them later.
Others wanted the version in the identifier: a URL ending in the version
(Kris Zyp),
or "bar/baz@foo:1.2"
(Irakli Gozalishvili).
We solve this with tooling for the web these days, but one can hope for
better.
If we make enough progress on a module loader API through Module Harmony at
TC39, I think we will close this gap.
Reading package.json at run time #
I always at least thought to myself that Node.js should read package.json
at run time and enforce the dependencies and devDependencies, such that
omitting one would cause a runtime error regardless of the layout of
node_modules, and missing dependencies would be discovered before
publishing and not after.
What I argued on the list was its nearest cousin, mappings.
Under Packages/Mappings/C, "the only
way to reference another package is through a mapping," as I
put it
in June 2010, so an undeclared dependency would simply not resolve.
My Lode, in 2011, did not support require.paths at all, "since packages
are statically linked"
(me).
Isaac argued the other side for npm, and won:
"There is no manifest. There is no runtime calculation or analysis
whatsoever. [...] The folder structure is the database," and "a package
manager should not be involved at run time"
(Isaac).
I still advocate for this in any system that can be designed that way, like
a bundler.
Linters help.
Of course, Node.js does read package.json now, for main, exports,
imports and type, the conditions
the third part traces back to
Narwhal's overlays.
I think that takes the wind out of the argument that it should not always
have, but it still does not read dependencies, and the ship has sailed.
So I am content with how CommonJS played out.
ESM, on the other hand #
When I proposed CommonJS to TC39 in January 2009, I acknowledged that it
was a stop-gap, and that a native implementation would be able to enforce
the static analyzability of the import graph.
In February I
showed
the list the "Future ES (our TC39 proposal)" form, import "B" as module1,
beside the transitional var module1 = require("B").
I think Brendan Eich and his fellow delegates only considered static,
synchronous module loading a viable way forward for the language proper.
We of course have both static and dynamic import in ESM, and I always
advocated for having both.
It was also always my position that CommonJS was primarily for the web. On February 3, 2009, I was already explaining how module loaders could manipulate scope "in modern browsers," and that Dojo and YUI would need an external loader to interoperate with Caja. In March I described "the high performance of prefetching and bundling" on the client, with "dodgy, synchronous fetching" left for the dynamic edge cases. The list was called ServerJS, though, and when Kevin Dangoor proposed renaming it, he wrote that "the API exists mostly to make server-side JS more productive, but there will inevitably be some spillover of functionality into the browser."
For the web, I fought to encourage the view that require must always
receive a statically analyzable module specifier string as its one and only
argument.
In August 2009 I was
favoring
require.async(id) returning a promise for the dynamic case, and
allowing a synchronous require to throw an error, "which may only happen
if the id is not a String literal."
In December, "Ihab and I would like there to be a clause that require must
be called with a string literal"
(me).
In October 2010, "We only had the foresight to restrict calls to require to
use string literal arguments, so that scraping would be possible"
(me).
Node.js overturned this and co-opted the vision, and I am content with that
outcome too.
Any library that sought to be portable to the web had to play ball with
tools like browserify, which necessarily resorted to a heuristic static
analysis of require calls, and that has had a herding effect on the
ecosystem nearly the same as universal enforcement of static require.
Node.js now uses a specific lexical analysis for named exports from
CommonJS, for linking with ESM, and could use the same tool to enforce
static require.
The Module constructor #
At that 2009 meeting we learned that TC39 had no appetite to move first on modules. Brendan's ruling was that they wanted me to go out and pitch this stop-gap, and find out whether the whole modules business had legs. Dave Herman opined that the only regrettable problem with letting the community lead was that it might succeed too well, and complicate a migration path to his particular vision. It transpires that modules did indeed have legs, and the migration has indeed proven complicated. So everyone can be congratulated for foresight.
In 2010, when people had been excited about Node.js for two whole months, I
presented again.
CommonJS did have legs, I said, but it was still a stop-gap, and the
language should have an enforced, statically analyzable import graph.
On January 24, 2010, I
wrote
on the list that CommonJS was "intended to be a transitional syntax for
future, natively supported "import" and "export" keywords," and that
var FOO = require("foo") would become const FOO = import "foo".
My vision was a Module constructor, a lot like the Function
constructor, that would produce a static analysis, accept its imports as a
map, and return the module's result:
"the "import" clause would enforce a string literal expression that would
be added to a "dependencies" array of the "Module" instance."
Destructuring, promises and async were science fiction at the time, but
in today's terms:
const dependent = new Module(`
const { x, y } = import "./dependency.js";
return { a: x + 1, b: y * 2 };
`);
const dependency = new Module(`
return { x: 2, y: 1 };
`);
dependent.dependencies[0] == './dependency.js';
const { a, b } = dependent({'./dependency.js': dependency()});
The migration path from CommonJS to this could have been walked with a
sed script.
It presented a very narrow profile to the committee, to minimize debate
and ship quickly enough to take the wind out of CommonJS early.
That did not happen.
Dave Herman and Sam Tobin-Hochstadt stayed up all night writing their
counter-proposal, which kept the door open for importing hygienic macros
rather than for migrating the nascent ecosystem quickly from CommonJS.
They called it "Simple Modules," which I found offensive, because the name
implied that mine was complex, and more so because it was not actually
simpler in any dimension.
I proposed a single form of import.
Simple Modules proposed all the import forms you see in ESM, and a
module keyword more like a C++ namespace, which obliged import to have
combinatoric semantics.
Still, I
pointed the list at it
in June 2010: "I had hoped that ES-Harmony would pick up a proposal with
static analysis of dependencies with a minimal migration path from
CommonJS," but also "I encourage everyone to take a good look at "Simple
Modules" and get involved."
TC39 sat on that proposal for something like five years, until Yehuda Katz
led a pragmatic conversation about forcing export default in, for parity
with Node's module.exports.
That broke the log jam, and opened the way for Rollup, Babel and
StandardThings/ESM to emulate ESM before browsers and Node adopted it, but
none of those emulations were interoperable, or even really migratable,
with one another.
At Agoric we had to fork StandardThings/ESM to migrate our packages to
Node.js one at a time, rather than all at once on a flag day.
A measure of responsibility #
I do not regret much about CommonJS. I grump occasionally that people sincerely believe, because of Node.js, that CommonJS was intended only for server-side JavaScript, without consideration for the web. That is a misunderstanding, and I understand where it comes from.
And I cannot say "I told you so" to anyone about these outcomes. The people who decided them knew very well that their choices would likely have these outcomes.
In 2010 I had not developed the emotional resilience to participate productively in TC39. After I was hurt by the implications of the name "simple modules," I took a step away, which was convenient because I did not represent an ECMA member, and invited experts were more constrained then than they are now. I stepped away from TC39, not from the list. I went on calling shows of hands through 2010 and into 2011, and in April 2010 I wrote "Strategy", which the first part quotes, about what CommonJS should do now that Node had its own momentum. I could have fought for a simpler outcome than the one we got. Instead I returned to my day jobs and vanished from the standards scene for many years. So I have a measure of responsibility for the outcomes we enjoy. I have very few regrets, an abundance of gratitude, and tremendous satisfaction that I played some part in making this enormous, vibrant and contentious ecosystem bigger than could fit in jQuery.
The JavaScript Modules Chronicle:
- The CommonJS Dialectic
- CommonJS Feature Discussions
- CommonJS Packaging
- CommonJS Retrospective
- Previous: Orders of Pretentiousness
- Next: Basic Binary Board Book