7

I have a fresh install of node.js running on Windows 7, and I am trying to run a very basic JQuery script, named a.js, which contains just:

require("jquery");
$().jquery;

Unfortunately, this will not run with JQuery, giving me a TypeError:

C:\Users\Ian>node a.js

C:\Users\Ian\node_modules\jquery\lib\node-jquery.js:10
    window.XMLHttpRequest.prototype.withCredentials = false;
                         ^
TypeError: Cannot read property 'prototype' of undefined
    at create (C:\Users\Ian\node_modules\jquery\lib\node-jquery.js:10:26)
    at C:\Users\Ian\node_modules\jquery\lib\node-jquery.js:9435:18
    at Object.<anonymous> (C:\Users\Ian\node_modules\jquery\lib\node-jquery.js:9437:2)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\Users\Ian\a.js:1:63)

I have found a few bug reports on this error through Google, most of which suggest downgrading JQuery. However, when I do that, I just get a different error. The below is with JQuery 1.6.3:

C:\Users\Ian>node a.js

module.js:340
    throw err;
      ^
Error: Cannot find module 'location'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at create (C:\Users\Ian\node_modules\jquery\node-jquery.js:6:33)
    at C:\Users\Ian\node_modules\jquery\node-jquery.js:9065:18
    at Object.<anonymous> (C:\Users\Ian\node_modules\jquery\node-jquery.js:9067:2)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

Can anyone suggest what may be wrong? I have tried various reinstallations of the software with no luck. I have tried both npm install -g and npm install for both the default version of jquery and also [email protected].

Edit: This question - as yet unanswered - seems related.

6
  • 2
    Why are you running jQuery on the server-side? Commented Oct 19, 2012 at 17:40
  • No good answer sorry @SLaks - I'm making use of someone else's code which runs JQuery on the server side. When I failed to get that working, I came up with my trivial test example to ensure that the problem was only with the JQuery part, and not anything else in the other person's code. Commented Oct 19, 2012 at 17:42
  • @Ina Yeah but that's the problem, jQuery is not a serverside library, it relies on stuff not present in node. Commented Oct 19, 2012 at 17:43
  • @JakubHampl so do you believe there is no workaround? I'm given to believe the code I inherited worked well enough. I can get some way along by manually installing packages (such as location, above) but I eventually run into a TypeError. I'd also appreciate any links you can provide around jQuery not being a serverside library, npmjs.org/package/jquery seems to imply it is, but I'm new to this and could easily be wrong. Commented Oct 19, 2012 at 17:55
  • @SLaks sometimes you re-use libraries written for the browser on the server-side. Commented Mar 21, 2013 at 3:16

5 Answers 5

8

If ur on node 0.10.x ... run sudo npm rebuild

https://github.com/coolaj86/node-jquery/issues/35

Sign up to request clarification or add additional context in comments.

1 Comment

This got me earlier as well. I was running ubuntu. Apt get only gets you 0.6. Upgraded to 0.10 and then this error. Cleaned out the node_modules and reran npm install.
3

In the git page for the jquery-nodejs project, says that does not works on Windows, so the problem is your OS. https://github.com/coolaj86/node-jquery

2 Comments

I'm having the same problem but on Mac OS X. Might have something to do with node 0.10.0 as I didn't see it happening before the upgrade.
This isn't really accurate: contextify doesn't build easily on Windows, but you can make it happen using node-gyp if you install the required MS build tools. Once you do that, jQuery should be installable.
0

You have to assign the module to the $ variable:

var $ = require('jquery');
$().jquery;

4 Comments

True, but the error happens on line 1, so it's not the undefined $.
Thanks @zeMirco, unfortunately that doesn't change any of my errors.
The error must be in the rest of your code that you didn't show us. The first error says window.XMLHttpRequest is undefined.
I just downgraded from node v0.8.12 to v0.8.11 but that hasn't helped me.
0

I ran into this issue on REHL and sudo npm rebuild solved the problem for me.

Comments

0

For those running Windows, the npm rebuild may not work and you might still see something like:

C:\Projects\NODE\paragraphs\node_modules\jquery\lib\node-jquery.js:10
window.XMLHttpRequest.prototype.withCredentials = false;
                     ^
TypeError: Cannot read property 'prototype' of undefined

Found this solution thanks to user NeverI that did the trick for me:

In node-jquery.js (found under \node_modules\jquery\lib) change the lines:

if(window == null ) {
    window = require('jsdom').jsdom().createWindow();

to:

 if(!window || !window.document) {
    window = require('jsdom').createWindow();
    window.document = require('jsdom').jsdom();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.