2

I've read previous threads where other members have had identical error messages as me, but their accepted solution doesn't seem to help.

Basically I get this in Chrome console when I try to load index.html:

Uncaught Error: Module name "underscore" has not been loaded yet for context: _. Use require([])

A couple of seconds later this shows up:

Uncaught Error: Load timeout for modules: underscore,backbone

Chrome network tools doesn't show any anomalies everything up until day_view.js is loaded fine (200 OK).

File structure

enter image description here

index.html

...
<script>
        var require = {
            deps: ["jquery/jquery-min", "underscore/underscore-min", "backbone/backbone-min"]
        };
</script>
<script data-main="scripts/main" src="scripts/require.js"></script>
...

main.js

require.config({
    baseUrl: 'scripts',

    paths:{
        jquery:'jquery/jquery-min',
        underscore:'underscore/underscore-min',
        backbone:'backbone/backbone-min'
    },

    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ["underscore/underscore-min", "jquery/jquery-min"],
            exports: "Backbone"
        }
    },

    waitSeconds: 200
});

require(['day_view'], function (day_view) {
    function start() {
        day_view.render();
    }

    return {
        start:start
    };
});

day_view.js

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
    function render() {
        ...
    }
...
MdaG
  • 2,680
  • 2
  • 34
  • 46
  • This question might help you: http://stackoverflow.com/questions/10866740/loading-jquery-underscore-and-backbone-using-requirejs-2-0-1-and-shim – jantimon May 27 '13 at 13:50
  • @jantimon I've read that one, but I can't see anything that helps with my current predicament. Is there anything in particular you think I should read? I've read this (http://requirejs.org/docs/errors.html#notloaded) too, but I can't find the cause there either. – MdaG May 27 '13 at 14:01
  • try `shim = { ... deps: ["underscore", "jquery"] ... }` – jantimon May 27 '13 at 14:05
  • @jantimon Doesn't work either. Same error message as in the question above. – MdaG May 27 '13 at 14:16
  • Maybe `define('day_view', ['jquery', 'underscore', 'backbone'], ...` – jantimon May 27 '13 at 14:33
  • try adding a slash in front of your `baseUrl`, like this `/scripts` – jakee May 27 '13 at 14:36
  • 1
    and also do what @jantimon said! – jakee May 27 '13 at 14:37
  • If I add the slash I need to make it "/html_client/scripts" or the path will be incorrect (Chrome says so). I adjusted the define in day_view.js to "define('day_view', ['jquery', 'underscore', 'backbone'], function($, _, Backbone) {". Still doesn't work. Same error message. Everything seems loaded in the correct order according to chrome and I can use jquery, backbone and underscore in the console. Is my use of require special in any way? I mean, it shouldn't be this much work to "install" require.js, right? I've been at it for 8 hours soon. :-) – MdaG May 27 '13 at 14:49
  • Wait a minute, I think you're right. It's not working (nothing is rendering), but I think you've answered my question and what I'm seeing now is something else. Care to make an answer that I can accept? :) – MdaG May 27 '13 at 15:00

3 Answers3

8

This finally worked.

require.config({
    paths:{
        jquery:'jquery/jquery-min',
        underscore:'underscore/underscore-min',
        backbone:'backbone/backbone-min'
    },
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        },
        waitSeconds: 15
    }
});

require(['day_view'], function (day_view) {
    function start() {
        day_view.show();
    }
    console.log(day_view); // Empty object here?
    return {
        start:start
    };
});

and

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) { ...
MdaG
  • 2,680
  • 2
  • 34
  • 46
3

You can use the shim above, or you can also use jrburke's** AMD compatible fork of Backbone/Underscore:

https://github.com/amdjs/backbone

https://github.com/amdjs/underscore

This allows you to simply do:

require.config({
    paths:{
        jquery:'jquery/jquery-min',
        underscore:'underscore/underscore-min',
        backbone:'backbone/backbone-min'
    }
});

define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) { ...

Frankly, I found the fork easier/cleaner/more robust than using a shim.

** FYI jrburke is the creator of requirejs.

Victor Quinn
  • 1,107
  • 9
  • 12
2

PS Good news, Underscore 1.6.0 now supports requirejs define !!!

versions below this require shims, or requiring underscore.js then blindly hoping that the "_" global variable hasn;t been smashed (which to be fair is a fair bet)

simply load it in by

  requirejs.config({
    paths: {
        "underscore": "PATH/underscore-1.6.0.min",
    }
  });

No shimming required :)

aqm
  • 2,942
  • 23
  • 30