I am learning about require.js and think I am just missing something. I don’t understand how it loads files.
I have my jquery file in a lib directory.
This does not work:
It shows that jquery is being loaded in the chrome network panel. Error is: Uncaught TypeError: undefined is not a function . so it is basically saying that $ is undefined.
require(['lib/jquery'],function($) {
$(document).ready(function(){
alert('hello');
});
});
This works:
require.config({
paths: {
jquery: 'lib/jquery'
}
});
require(['jquery'],function($) {
$(document).ready(function(){
alert('hello');
});
});
In other examples I see online you don’t have to set the paths with require.config. Do I have to do this every time that I want to define a module? I know I am using require and not define in this case, but I am having the same issue with the define method. Every time I make a module using define I have to set the paths using require.config(). I think I am missing something here. Can anyone point me in the right direction?
Update to reflect comments and James’ answer:
You have two problems:
jQuery, for the reasons James outlines in his answer, requires that you either have
pathsset in your config, or that you, in the code you’ve outlined, setbaseUrlto “lib”.you have to remember that you can’t just load any old script with RequireJS. Only scripts that conform to the AMD standard can be loaded.
Having said this, I’d advice you use require-jquery instead.
You’ll probably end up using jQuery plugins that will assume jQuery is loaded on the page, and those won’t work with the approach you’re trying.