Use requirejs and jquery, without clobbering global jquery?

jQuery 1.7 supports AMD loading. But, the trick is avoiding a module naming conflict, since jQuery hardcodes its own module name as ‘jquery’. If you are defining another module as ‘jquery’ (say, in the ‘paths’ property in your requirejs config), it will cause said conflict.

To load jQuery in your requirejs application scope, define a module, such as ‘jquery-loader’ and when you want to load jQuery, do it via ‘jquery-loader’:

define(['path/to/jquery.min'], function () {
    return jQuery.noConflict(true);
});

requirejs will ‘cache’ your reference to jQuery so that noConflict will only run the first time jquery-loader is loaded.

Leave a Comment