最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Requirejs: jQuery is undefined - Stack Overflow

programmeradmin1浏览0评论

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/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?

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./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?

Share Improve this question edited Jun 10, 2013 at 5:59 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jun 10, 2013 at 5:35 Alex BeaucheminAlex Beauchemin 1,1811 gold badge17 silver badges25 bronze badges 3
  • 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 Commented Jun 10, 2013 at 5:47
  • I tried adding 'http:' but still the same error. The file is loading fine without the 'http:' too. – Alex Beauchemin Commented Jun 10, 2013 at 6:34
  • what file is the error Uncaught ReferenceError: jQuery is not defined originating from, can you see that in your console ? – Willem D'Haeseleer Commented Jun 10, 2013 at 6:35
Add a ment  | 

1 Answer 1

Reset to default 4

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./requirejs/example-jquery-shim#how-its-set-up

发布评论

评论列表(0)

  1. 暂无评论