require.undef is not synchronous with define #678

itay opened this Issue Mar 22, 2013 · 1 comment

2 participants


If I have a define call and immediately try and undefine it, then that doesn't work. For example:

<script src=""></script>
<script src=""></script>

    // This will work, even though common sense says it should not
    //require(["jquery"], function(jq) {
    //    console.log(jq === $); 

    // This will give an exception:
    // Uncaught Error: Module name "jquery" has not been loaded yet for context: _. Use require([])

    // This will kind of work, but will still cause an exception:
    //GET 404 (NOT FOUND) require.js:1854
    //Uncaught Error: Script error

The specific case where this happens to me is in Django, when I use the Django Debug Toolbar. They include their own version of jQuery, and use jQuery.noConflict(true) to have it not interfere with the jQuery that may already be on the page.

However, jQuery has its own call to define:

define( "jquery", [], function () { return jQuery; } );

which I would like to undefine, otherwise my calls to require or define end up picking up this jQuery, which is not what I want. However, as noted above, there is no way for me to undefine it synchronously, so I have to do something like:

require(['jquery'], function() {

which seems both silly and dangerous, because something else still has time to pick up the bad jquery value.

requirejs member

Why not use the first version you mention:

require(["jquery"], function(jq) {
    console.log(jq === $); 

This works because the script tag jquery has called define(), so the undef gets rid of it, and then you can continue with the load of jquery that you want.

I think this ticket is more about why things work they way they do than a specific code bug, so closing as part of bug triage. However, feel free to continue discussion and we can reopen if we uncover something to be fixed.

@jrburke jrburke closed this Mar 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment