Module naming and declarations

Domenic Denicola domenic at domenicdenicola.com
Tue May 7 17:21:05 PDT 2013


I think one point that's being hinted at, but not explicitly called out, is the confusing nature of `import "foo"` in the proposed scheme. Notably, it shares this confusion with AMD, but not with Node.js.

The problem is that `import "foo"` can mean *either* "import the main module for package with name `foo`" *or* "import `foo.js` resolved relative to the base URL".

In AMD, this problem has been a constant headache on projects that I've worked on at my jobs. In Node.js, however, `import "foo"` always means the former, and never the latter—Node.js has no concept of "base URL." Instead it has the option of doing `import "./foo"`, which has different semantics: "import `foo.js` relative to the module in which this code is found."

One can summarize the Node.js scheme as "if it starts with a dot, use relative resolution; otherwise, delegate to the package manager." This solves the problem nicely of preventing anyone from confusing module IDs with URLs, since although technically the start-with-dot subset could be interpreted as URLs, it's not common to see URLs like that.

I'm not sure the Node.js scheme is the best idea for the web, but I *would* like to emphasize that the AMD scheme is not a good one and causes exactly the confusion we're discussing here between URLs and module IDs.


More information about the es-discuss mailing list