Let's say you have the following directory structure, and the following files:
root
|-- require-jquery.js
+-- folder
|-- index.html
|-- main.js
+-- AnotherModule.js
In RequireJS, when you reference a module starting with a ".", RequireJS looks in the same folder that your current module is in, even if that's a subdirectory. However, if you change baseUrl just before calling define(), RequireJS will map dependencies to the new baseUrl.
You can fix this by setting baseUrl in index.html and changing data-main to a path relative to baseUrl:
folder/index.html:
<script>
var require = {
baseUrl : "../"
};
</script>
<script data-main="folder/main" src="../require-jquery.js"></script>
folder/main.js:
define(
[ "jquery", './AnotherModule' ],
function($, AnotherModule) {});
This only works for define(), though:
folder/main.js:
require(
[ "jquery", './AnotherModule' ],
function($, AnotherModule) {});
if I try it with require(), RequireJS will look for AnotherModule.js in root, not folder. Why is this, and in particular, why the design difference between define() and require()?