Organizational Research By

Surprising Reserch Topic

error jquery requires a window with a document

error jquery requires a window with a document  using -'jquery,node.js'

So everything was working just fine and great until doing npm update and now things do not work quite as they used to.

A little background: in my code I use jquery to parse textual html. I do not use a window and I don't use jsdom. It used to work just fine to do this:

$ = require("jquery");
$(html).find("<h1>").html("The title");

But now I get this:
jQuery requires a window with a document

How do I fix this?

asked Sep 18, 2015 by deepak gupta
0 votes

Related Hot Questions

6 Answers

0 votes

node.js-jQuery definition in one line:

// definition
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);

// usage
"); console.log($("body").html());
answered Sep 18, 2015 by okesh.badhiye
0 votes

I solved it. had to first remove jsdom and jquery then npm install jsdom jquery.

Then this:

var jsdom = require("jsdom"); 
$ = require("jquery")(jsdom.jsdom().createWindow()); 

Turns out that it was important that I had the latest version. Otherwise it didn't work..

answered Sep 18, 2015 by dahiyabecomp
0 votes

You can try the following with these version settings in your package.json:

"jquery": "^2.1.1",
"jsdom": "^1.0.0-pre.3"

Say you have a file called page.htm:



Html page

left content

Then, you can read from file, create a DOM and window from it with JSDOM and pass it to jquery:

var jsdom = require("jsdom").jsdom;
var fs = require('fs');
fs.readFile('page.htm', {encoding: "utf8"}, function (err, markup) {
  if (err) throw err;
  var doc = jsdom(markup);
  var window = doc.parentWindow;
  var $ = require('jquery')(window)
  var outerLeft = $(".left").clone().wrap('
').parent().html(); var innerLeft = $(".left").html(); console.log(outerLeft, "and ...", innerLeft); });

will output:

left content

and ...

left content

answered Sep 18, 2015 by atulpariharmca
0 votes

The npm package for jQuery definitely used to include jsdom with it, which is why it would work at all in Node: jQuery needs to have some kind of DOM environment to work with.

You can check that old versions used to have it by doing npm install jquery@1.8.3. You'll see jsdom being installed with it. For some reason they seem to have removed jsdom from the recent releases. I don't know why.

However, using jsdom to run jQuery code is simple:

var jsdom = require("jsdom");
var window = jsdom.jsdom().parentWindow;

jsdom.jQueryify(window, "", function () {
  var $ = window.$;

The title

"); console.log($("h1").html()); });

The path could be changed to a different version of jQuery or to a local file.

answered Sep 18, 2015 by bhavin
0 votes

I plopped this on the top of my Node.js (server) file that uses jQuery to help render html text ...

var jsdom = require("jsdom").jsdom;
    html : "",
    done : function(errs, window) {
        global.window = window;

and everything comes up roses.

answered Sep 18, 2015 by r3tt
0 votes

You have to provide a window object. To do this, just pass parentWindow from jsdom:

var jsdom = require("jsdom"); 
var $ = require("jquery")(jsdom.jsdom().parentWindow);
answered Sep 18, 2015 by balvant maurya