I'm using requirejs to load some libraries and dependencies.
When I just load jQuery, it's working perfectly:
main.js
require.config({
shim: {
jquery: {
exports: '$'
}
},
paths: {
jquery: 'vendor/jquery'
}
});
require([
'vendor/jquery',
'app/init'
]);
app/init.js
define(
['jquery'],
function ($) {
$(document).ready(function () {
console.log('domready');
})
}
)
But when I try to add underscore, in the network panel the file is correctly loaded but in the console I get a
Uncaught Error: Load timeout for modules: underscore
What's happening? I also tried the waitSeconds: 200 options inside the require.config without any success.
Below the final (broken) code as a reference:
main.js
require.config({
shim: {
jquery: {
exports: '$'
},
underscore: {
exports: '_'
}
},
paths: {
jquery: 'vendor/jquery',
underscore: 'vendor/underscore',
}
})
require([
'vendor/jquery',
'vendor/underscore',
'app/init'
])
app/init.js
define(
['jquery', 'underscore'],
function ($, _) {
$(document).ready(function () {
console.log('domready');
})
}
)
I'm using requirejs to load some libraries and dependencies.
When I just load jQuery, it's working perfectly:
main.js
require.config({
shim: {
jquery: {
exports: '$'
}
},
paths: {
jquery: 'vendor/jquery'
}
});
require([
'vendor/jquery',
'app/init'
]);
app/init.js
define(
['jquery'],
function ($) {
$(document).ready(function () {
console.log('domready');
})
}
)
But when I try to add underscore, in the network panel the file is correctly loaded but in the console I get a
Uncaught Error: Load timeout for modules: underscore
What's happening? I also tried the waitSeconds: 200 options inside the require.config without any success.
Below the final (broken) code as a reference:
main.js
require.config({
shim: {
jquery: {
exports: '$'
},
underscore: {
exports: '_'
}
},
paths: {
jquery: 'vendor/jquery',
underscore: 'vendor/underscore',
}
})
require([
'vendor/jquery',
'vendor/underscore',
'app/init'
])
app/init.js
define(
['jquery', 'underscore'],
function ($, _) {
$(document).ready(function () {
console.log('domready');
})
}
)
Share
Improve this question
asked Nov 29, 2013 at 14:30
LukeLuke
3,0467 gold badges35 silver badges45 bronze badges
2
- I have the same problem , right now im catching the error and checking its type if its timeout and if it is , then reloading the browser .Hope someone helps out – Rana Deep Commented Nov 29, 2013 at 14:32
- @RanaDeep I hope it too, I tried to load another library instead of underscore (i.e. iScroll) but I'm still getting the same error. – Luke Commented Nov 29, 2013 at 14:37
1 Answer
Reset to default 11In define
and require
calls you sometimes refer to your modules as "vendor/<name of module>"
and sometimes as "<name of module>"
. This is wrong. Based on the config you show you should refer to your modules as "<name of module>"
in all require
and define
calls. So always refer to jQuery and Underscore as "jquery"
and "underscore"
, not "vendor/...
. When you refer to them with the full path you bypass the shim
configuration.
Actually, you can change your require
call to:
require(['app/init']);
You don't need to refer to jQuery and Underscore there. They will be loaded when app/init.js
is loaded due to the define
there requiring them.
(Also, relatively recent versions of jQuery don't need a shim because jQuery detects that it is loaded by an AMD-patible loader and calls define
itself. This is not the source of your problem but it is good to know.)