7

I am trying to get jsdom to work :)

Here's the code:

var jsdom = require("jsdom");
var request = require("request");
var fs     = require('fs');
var jquery = fs.readFileSync("./jquery-1.7.2.js", 'utf-8');

request({ uri:'http://nodejs.org/dist/' }, function (error, response, body) {
    if (error && response.statusCode !== 200) {
        console.log('HTTP request error... '+error);
    }
    jsdom.env({
        html: body,
        scripts: [
            jquery
        ],
        done: function(errors, window) {
            console.log('done');
        }
    });
});

And here's the error:

jsdom.js:171
    features   = JSON.parse(JSON.stringify(window.document.implementation._fea
                                                          ^
TypeError: Cannot read property 'implementation' of undefined

I have checked if the page is fetched and if the jquery lib is parsed - they are.

We could look at the implementation of jsdom.js:

[snip]
exports.env = exports.jsdom.env = function() {
    [snip]
    window     = exports.html(html, null, options).createWindow(),
    features   = JSON.parse(JSON.stringify(window.document.implementation._features)),
    docsLoaded = 0,
    [snip]

It seems that the .createWindow() is not successful...

And I am running it on Cloud9.

Any help is welcome.

Mikeumus
  • 3,570
  • 9
  • 40
  • 65
pour toi
  • 1,026
  • 2
  • 13
  • 25
  • What version of node and jsdom? It works here with node v0.6.16 and jsdom 0.2.14. (By the way, you could do `fs.readFileSync(path, 'utf8')` instead of the `toString()` call (to which you also probably should pass `'utf8'`)). – Linus Thiel Apr 13 '12 at 16:36
  • The version of node is 0.6.8 and the version of jsdom is 0.2.14. – pour toi Apr 15 '12 at 09:55
  • if the code is from server side i think "window" is only from browser side – ZiTAL Apr 16 '12 at 07:21
  • The idea is to have server-side access the|a DOM - https://github.com/tmpvar/jsdom ... – pour toi Apr 16 '12 at 07:37

3 Answers3

7

As reported at https://github.com/tmpvar/jsdom/issues/436, this can be caused by contextify not being installed. In theory jsdom has a shim that allows it to work without having contextify which is why it has it listed as a optional rather than required module, but this shim seems to be broken (see https://github.com/tmpvar/jsdom/issues/456).

So as of right now you need to get contextify installed to use the latest jsdom. Depending on your OS and environment there are several reasons that could be a problem. Run npm install contextify and then track down that issue.

Things to try depending on what your root issue is: - Verify you have Python 2.7 or greater and optimally 2.7.3 installed. Also verify its the one that gets run when you type python --version and that your PYTHONPATH is correct. - sudo npm install contextify - npm cache clean; npm install contextify --force

studgeek
  • 14,272
  • 6
  • 84
  • 96
  • Thanks for the link to the issues discussion. After hitting this problem I'm thinking of switching to Domino/Zepto Node, to avoid the Python dependency. – Jonathan Nov 15 '12 at 06:23
  • I had the same problem. install contextify and upgrading jsdom did do it: `sudo npm install contextify; npm ls; npm remove jsdom; npm install jsdom@0.5.4; npm cache clean` – dr jerry Nov 09 '13 at 21:56
0

I found the answer to the question.

A piece of it can be found on http://support.cloud9ide.com/entries/21243717-accessing-your-private-running-project-via-javascript-or-curl .

So, in order to make jsdom work on cloud9, one needs to use an older version (like 0.2.1) and run the script with an older version of node (like 0.4, which is supported on cloud9).

pour toi
  • 1,026
  • 2
  • 13
  • 25
0

Try upgrading jsdom. I had exactly the same error with an old version of jsdom (0.3.x). Then I removed it and installed the current (jsdom@0.6.5) version and now it works.

ecdeveloper
  • 2,777
  • 1
  • 22
  • 16