RequireJS: module ID vs module name

2.1k views Asked by At

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.

1

There are 1 answers

0
Louis On BEST ANSWER

Module name and module id are the same thing, and they are different form the module path. Suppose the following configuration:

require.config({
    baseUrl: '/lib/',
    paths  : {
        bar        : 'a/b/c',
        flip       : 'd/e/f',
        'flip/flop': 'dir/dir/something'
    }
});

Your first quote talks about what happens when you call something like require(['foo'], .... There is no paths in the configuration above that specifies what foo translates to. So RequireJS will create a path from the module id, which is foo. 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 you require(['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 do require(['bar/baz'], .... With the configuration above, RequireJS would split the module id in two: bar, and baz, would find that bar has a paths configuration and so would build the path /lib/a/b/c and then would add baz 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 your paths 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 combine flip/flop with .. which resolves to flip, and then RequireJS will convert this module id to a path: d/e/f.js because flip has a mapping in the paths. 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 load dir/dir.js.)