Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Module name “underscore” has not been loaded yet for context: _ #121

Closed
mic0331 opened this Issue Dec 28, 2012 · 6 comments

Comments

Projects
None yet
3 participants

mic0331 commented Dec 28, 2012

Hello,

I've been playing with jam since few days now and got an unexpected error.

here is my package.json file

    "jam": {
      "packageDir": "public/lib",
      "baseUrl": "public",
      "dependencies": {
        "backbone": "*",
        "jquery": "*"
      }
    }

When i run my website i have no issue, all files are loaded correctly...
As soon as I compile everything the browser throw this error

Uncaught Error: Module name “underscore” has not been loaded yet for context: _

my compile command is the following

jam compile -i app -o public/lib/require.js 

as soon as i run jam recompile everything works fine.

Any idea ?
Thanks for your help

I'm having the same issue. It seems that the dependencies for non-AMD scripts specified in shim are not being resolved by the compiler.

I've managed to get my app compiled into a single file by excluding underscore, jquery, backbone from my define module dependencies, leaving them as global objects, and explicitly including them before my app like so:

jam compile -i underscore -i jquery -i backbone -i app -o public/scripts/lib/require.js

This creates the opposite problem, though, as then the uncompiled scripts are broken, since _ and Backbone are never loaded in that case.

A workable, though still less-than-ideal, workaround is to compile underscore-jquery-backbone to a separate backbone-standalone.js and add it to the page before require.js, and just refer to _, $, Backbone as globals:

<script src="scripts/non-amd/backbone-standalone.js"></script>
<script src="scripts/lib/require.js"></script>

This at least works in both uncompiled and compiled scenarios, but the downside is that we can only compile down to 2 scripts instead of 1.

Contributor

jrf0110 commented Jan 25, 2013

The way I got around this was to put any third party libs like jquery, underscore and backbone under a common namespace called utils. I'd have to use the global variable for defining Backbone and underscore like so:

define(function(require){
   var
    // 3rd Party Dependencies
    $         = require('jquery')
  , _         = require('underscore') || window._ // In case it exists as a global already use that
  , domready  = require('domReady')
    // tell requirejs that backbone is a dep, but use the global one
    // (backbone's shim, when compiled, provides a bad value to require
    //  so we're better off using the global)
  , bb        = require('backbone')

    // Module Variables
  , utils     = {}
  ;

  utils.noop = function(){};

  utils.dom = $;

  utils.Backbone   = Backbone;
  utils.Events     = Backbone.Events;
  utils.Router     = Backbone.Router;
  utils.Model      = Backbone.Model;
  utils.View       = Backbone.View;
  utils.Collection = Backbone.Collection;
  utils.History    = Backbone.History;

  utils = _.extend(utils, _);

  return utils;
});

Then whenever I need to use Backbone or underscore, I just use my utils module. This isn't so bad if you start your project off doing it this way. Pretty lame.

mic0331 commented Jan 26, 2013

Hi,

thanks for your reply, your suggested common namespace has solved the issue. The thing that i don't understand is this section

utils.noop = function(){};

utils.dom = $;

utils.Backbone   = Backbone;
utils.Events     = Backbone.Events;
utils.Router     = Backbone.Router;
utils.Model      = Backbone.Model;
utils.View       = Backbone.View;
utils.Collection = Backbone.Collection;
utils.History    = Backbone.History;

why are you use "Backbone" and not "bb" ? What is the noop function used for ?
By the way, why are you using 'domReady' ?
Thanks again, this makes me moving a big step forward ;-)

Contributor

jrf0110 commented Jan 28, 2013

It's really just the difference between using the shimmed value and the global value. The shim value seems to be invalid after compilation, hence your issue. I should have changed that line to be like what I did for underscore. So something like:

var
  // 3rd Party Dependencies
  $         = require('jquery')
, _         = require('underscore') || window._ // In case it exists as a global already use that
, Backbone  = require('backbone')   || window.Backbone
, domready  = require('domReady')

  // Module Variables
, utils     = {}
;
Contributor

jrf0110 commented Jan 28, 2013

p.s. - When making a code block in markdown, put the syntax you want to use after the three opening ticks.

'''javascript
{{code}}
'''

To illustrate the syntax, I used apostrophes.

Also, glad I could help :D

mic0331 commented Jan 29, 2013

thanks for that @jrf0110, i will use the syntax for my next posts.

@mic0331 mic0331 closed this Jan 29, 2013

@jrf0110 jrf0110 referenced this issue in jashkenas/backbone Jun 27, 2013

Closed

Reconsider AMD support #2641

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment