4

I always get this error when I freshly load my app,

Error: Module name "underscore" has not been loaded yet for context:
_. Use require([]) http://requirejs.org/docs/errors.html#notloaded  

...,h){c=Error(c+"\nhttp://requirejs.org/docs/errors.html#"+b);c.requireType=b;c.re...

require.js (line 8) TypeError: Backbone.Model is undefined  

var ProjectModel = Backbone.Model.extend({

But then they are gone when I hit the refresh button on my browser.

Does anyone know why? How can I fix it?

This is my config/ main/ entry js file,

require.config({
    //By default load any module IDs from js/lib
    baseUrl: 'js',

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

    shim: {
        jquery: {
          exports: '$'
        },
        underscore: {
          exports: '_'
        },
        backbone: {
          exports: 'Backbone'
        }
      }
});

require([
    // Load our app module and pass it to our definition function
    'app',
    'jquery',
    'underscore',
    'backbone',

    // Pull in the Collection module.
    'collection/contacts'
], function(App){
    // The "app" dependency is passed in as "App"
    App.initialize();
});

Have I missed something?

enter image description here

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
Run
  • 54,938
  • 169
  • 450
  • 748

1 Answers1

4

try this:

shim: {
        jquery: {
          exports: '$'
        },
        underscore: {
          deps:["jquery"],
          exports: '_'
        },
        backbone: {
          deps:["jquery"],
          exports: 'Backbone'
        }
      }
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • thank you! it works beautifully now! can I ask why this change makes such a big different - what does `deps:[...],` mean? thanks! – Run Dec 01 '13 at 10:49
  • 1
    deps define the dependencies of AMD modules, because you don't have the "instance" of jquery for example inside backbone, and you tell that backbone depends from jquery. Glad to help you :) @lauthiamkok – Alessandro Minoccheri Dec 01 '13 at 10:52
  • 1
    Unless loading an ancient version of jquery, jquery needs no shim and shimming it *could* eventually lead to problems down the road. – Louis Dec 01 '13 at 12:45
  • This seems like a duplicate: http://stackoverflow.com/questions/16774214/trouble-combining-require-js-and-backbone-js-underscore-js Adding the backbone dependencies also helped me: backbone: { deps: ["underscore", "jquery"], exports: "Backbone" }, – Chris Jan 27 '14 at 07:09