The problem is that relative dependencies are loaded relative to the name of your module rather than its path. You refer to this example:
//my/shirt.js now has some dependencies, a cart and inventory
//module in the same directory as shirt.js
define(["./cart", "./inventory"], function(cart, inventory) {
This will work only if the my/shirt.js
file is loaded as the my/shirt
module. In other words, there is an assumption in this example that there is no paths
setting that will change the modules' paths.
Here is what happens with your code:
RequireJS loads
main
. You don't set abaseUrl
so thebaseUrl
is set to/lib
.You require
my
. RequireJS resolves it to/lib/vendor/MyModule/my.js
and loads it.The
my
module requires./lib/a
and./lib/b
. When RequireJS resolves the relative paths, it uses the current module's name (my
) as its starting point. The initial period in the two requirements means "get the directory part of the current module name and add the rest of the path to it." The directory part ofmy
is the empty string, so the two paths becomelib/a
andlib/b
and then are combined with the baseUrl to form/lib/lib/a
and/lib/lib/b
.Note that the
paths
setting formy
is not used at all in this computation.
One way to get around your issue would be to use map
instead of paths
. Whereas paths
associates paths to module names, map
associate module names to module names so:
map: {
'*': {
my: 'vendor/MyModule/my'
}
}
This tells RequireJS that in all modules (*
) if the module with name my
is required, the module with name vendor/MyModule/my
is loaded.