Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency on 'window' breaks in nodejs #4

Closed
absoludity opened this issue Mar 3, 2012 · 9 comments
Closed

Dependency on 'window' breaks in nodejs #4

absoludity opened this issue Mar 3, 2012 · 9 comments

Comments

@absoludity
Copy link

Hey there :) I think you're already aware of this by the comment in the code:

// Use window for now (maybe this?)

but I just tried a few ways to get the use plugin working with node, but didn't manage.

< ReferenceError: window is not defined
< at /home/michael/dev/ogt/backbone-test/app/scripts/libs/requirejs/plugins/use.js:52:21
< at Function.execCb (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:1872:25)
< at execManager (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:541:31)
< at /home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:642:25
< at execManager (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:619:17)
< at main (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:944:17)
< at callDefMain (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:955:18)
< at Object.completeLoad (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:1534:21)
< at Function.load (/home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:2286:17)
< at /home/michael/dev/ogt/backbone-test/node_modules/requirejs/bin/r.js:1281:37

@tbranyen
Copy link
Owner

tbranyen commented Mar 3, 2012

Can you test by modifying the lib to look like this:

(function(window) {

/* Keep source code the same */

})(typeof window == "undefined" ? global : window);
or 
})(this);

And let me know if that works or fails.

@absoludity
Copy link
Author

Hey there :). So wrapping the use plugin as above does indeed remove the "window is not defined" traceback, so the tests actually run, but it's not being defined... that is:

define(['use!expect'], function(expect){                       
    var assert = require('assert');                                                                                            

    suite("Using the 'use' plugin in node", function(){        

        test("it should be defined", function() { 
            assert(expect !== undefined);
        });
    });
}); 

fails. So I guess it'll require a bit more thought for nodejs support. If you've got an idea and enough time to dump it here, I can have a go. I'd already tried adding:

var attach_to;
if (window !== undefined){
    attach_to = window
} else {
    attach_to = root;
}

and doing s/window/attach_to in use.js, but it had the same effect as your solution (the test would run, but 'expect' wasn't defined).

Thanks either way.

@iaincarsberg
Copy link

Hey I needed to get use.js working in node, if your still having issues might be worth checking out iaincarsberg@fcfe816

I've only tested it with underscore and flow-js, so the "|| exports[]" change may need expanding depending on how other libraries work.

Once I've had time to test it with more libraries I'll submit a pull request.

@devertex
Copy link

I got the same issue while using the "use!" plugin with require.js on node. The setup is relatively complex, the plugin being configured for backbone, underscore and jqueryUI. The first tests of the modification proposed by @iaincarsberg is really promising.

@idflood
Copy link

idflood commented Apr 17, 2012

I've tested a little more in depth the modification, and wasn't able to load jqueryUI library. The error reported is this:

... src/scripts/libs/jquery-ui/js/jquery-ui-1.9m6.js as module "jQueryUi" failed with error: ReferenceError: jQuery is not defined

the configuration used in require.js

paths:
      jQuery: "libs/jquery-1.7.2"
      jQueryUi: "libs/jquery-ui/js/jquery-ui-1.9m6"
      Underscore: "libs/underscore"
      Backbone: "libs/backbone"

use:
      jQuery:
        attach: "jQuery"
      Underscore:
        attach: "_"

      Backbone:
        deps: [ "use!Underscore", "jQuery" ]
        attach: "Backbone"

      jQueryUi:
        deps: [ "use!jQuery" ]
        attach: "jQuery"

I will try to dig into this issue this week, will see if I find something.

@tbranyen
Copy link
Owner

@idflood jquery is a named lib jquery you have jQuery does changing that to lowercase help?

@idflood
Copy link

idflood commented Apr 18, 2012

@tbranyen : Thanks, I was just working on this, and found that manually loading jquery before jquery-ui helped a lot. I came to this solution after seeing that jQuery is attached to the window object:

requirejs("jQuery")
global.jQuery = global.$ = window.jQuery

Here is the file where all the black magic happens: https://github.com/idflood/ThreeNodes.js/blob/mocha/src/scripts/testrunner.coffee

But I'm starting to add a more and more inelegant code in the testrunner (and classes in src/scripts/threenodes but that is a little off topic). I will try your suggestion : )

@wachunga
Copy link

This issue is also affecting me. I had to undo incorporating use.js and go back to re-wrapping my non-AMD third party libs for now because I'm using them in the browser and node.

@tbranyen
Copy link
Owner

tbranyen commented May 2, 2012

Fixed in the latest build.

@tbranyen tbranyen closed this as completed May 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants