5
votes

Somehow I always receive this error:

Uncaught ReferenceError: jQuery is not defined

I have the impression that jQuery (loaded via CDN) takes more time to load (confirmed by the network tab on Chrome). I run this locally on my PC, so that's why the CDN call will always be longer then the libraries. But isn't requirejs supposed to wait after jQuery is loaded before loading the other libraries?

My boot.js :

(function(){
    requirejs.config({
        baseUrl: '/assets/js/',
        paths: {
            'lib': 'lib/',
            'src': 'src/',
            'jquery': [
                '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
                'lib/jquery-1.9.1.min'
            ],
            'mootools': 'lib/mootools-core-1.4.5',
            'class.mutators': 'lib/Class.Mutators.jQuery'
            //'order': 'assets/js/lib/order',
        },
        shim: {
            'class.mutators': {
                deps: [
                    'mootools'
                ],
                exports: 'classmutators'
            },
            'underscore': {
                    exports: '_'
            }
        },
        waitSeconds: 15
    });

    requirejs([
        'jquery',
        'src/app-require'
    ], function($){
        $(document).ready(function(){
            var App = new $.App($('body'));
        });
    });

})();

My app-require.js :

define([
    'mootools',
    'class.mutators',
    'src/Tracker',
    'lib/jquery.easing.1.3',
    'lib/nivo/jquery.nivo.slider.pack',
    'lib/isotope/jquery.isotope.min',
    'lib/waypoints.min'
], function() {

var className = 'App';
//--
return $[className] = new Class({
    jQuery: className,

    Implements: [Options, Events],

    options: {},

    //-- init
    //---------------------------------------------
    initialize: function(el, options) {
              ...
    },

            ...
    });
});

Any ideas?

1
use http:// in your jQuery url when you are accessing the app via chrome from local. because chrome appends file:// in your url when accessing hence not loading jquery when accessing from your local. instead you should install a webserver to access your app through localhost - Kishore
I tried adding 'http:' but still the same error. The file is loading fine without the 'http:' too. - Alex Beauchemin
what file is the error Uncaught ReferenceError: jQuery is not defined originating from, can you see that in your console ? - Willem D'Haeseleer

1 Answers

4
votes

jQuery is not in any dependencies. You should add it in the shim dependencies of mutator:

  shim: {
            'class.mutators': {
                deps: [
                    'jquery',
                    'mootools'
                ],
                exports: 'classmutators'
            },
            'underscore': {
                    exports: '_'
            }
        },

Here is the official example of how to handle a jQuery dependency with shim: https://github.com/requirejs/example-jquery-shim#how-its-set-up