I have a simpliest project using requirejs. A data-main file is js/default.js
You can see there two jQuery files. RequireJS can see only that which is located on the same level that default.js So this code:
require(
['jquery', 'modules/module1', 'modules/module2'],
function($, Module1, Module2){
$('body')
.append(Module1.phrase)
.append('<hr/>')
.append(Module2.ask);
}
);
...works
But if I change ['jquery', ... to ['libs/jquery', ... , it reveals an error:
Uncaught TypeError: $ is not a function
What's wrong here? Whole code is here: http://plnkr.co/edit/QuFsKN597RD873MOlUw8?p=preview
jQuery must indeed be required as jquery
.
Now, there may be still confusion regarding the module name. Someone may look at the dependency libs/jquery
and infer that jQuery is in fact required as jquery
. That is, doing require(['libs/jquery'], ...
would be asking RequireJS to load the module jquery
located in libs/
. However, this is not the case. The module name of the module being required is the entire name listed in the dependency list. So in the require
call above, the module name of the module required is libs/jquery
. But jQuery calls define('jquery', ...
to declare itself to RequireJS. The module name (the first argument is jquery
, not libs/jquery
and thus RequireJS fails to find the module requested.
A trivial fix is to add a paths
configuration:
paths: {
jquery: 'libs/jquery'
}