Errors using JQuery with node.js

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 jquery@1.6.3.

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

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

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

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

You have to assign the module to the $ variable:

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

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

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();