I'm kinda noob in RequireJS; I recently read the API documentation, and came across these two terms: module ID
and module name
. Are they used interchangeably? Or are they somehow different concepts?
Excerpts:
http://requirejs.org/docs/api.html#jsfiles
RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs. RequireJS will automatically add it when translating the module ID to a path.
http://requirejs.org/docs/api.html#config-paths
The path that is used for a module name should not include an extension, since the path mapping could be for a directory. The path mapping code will automatically add the .js extension when mapping the module name to a path.
http://requirejs.org/docs/api.html#modulenotes
The loader stores modules by their name and not by their path internally. So for relative name references, those are resolved relative to the module name making the reference, then that module name, or ID, is converted to a path if needs to be loaded.
Module name and module id are the same thing, and they are different form the module path. Suppose the following configuration:
Your first quote talks about what happens when you call something like
require(['foo'], ...
. There is nopaths
in the configuration above that specifies whatfoo
translates to. So RequireJS will create a path from the module id, which isfoo
. Ultimately it will try to load the file/lib/foo.js
.Your second quote talks about what happens when there is a
paths
for your module. If yourequire(['bar'], ...
then RequireJS will transform the id to/lib/a/b/c.js
when it tries to load it. It adds the extension itself. This same quote also obliquely alludes to the case where you'd dorequire(['bar/baz'], ...
. With the configuration above, RequireJS would split the module id in two:bar
, andbaz
, would find thatbar
has apaths
configuration and so would build the path/lib/a/b/c
and then would addbaz
to it and the extension so it would try to load the file/lib/a/b/c/baz.js
. So if you have hierarchy of related modules you can just put the root of that hierarchy in yourpaths
rather than specify a path for each and every module in the hierarchy.The third quote points out that the module id is what is used when interpreting relative module ids. Let's say
flip/flop
has been loaded and it has..
in its dependencies. RequireJS will combineflip/flop
with..
which resolves toflip
, and then RequireJS will convert this module id to a path:d/e/f.js
becauseflip
has a mapping in thepaths
. Sometimes people think that RequireJS will interpret..
relative to the path of the module that needs it. The quote clarifies that it is not the case. (If it were the case, then RequireJS would try to loaddir/dir.js
.)