Search code examples
javascriptnode.jsjsdomcloud9-idecontextify

Node.js jsdom error


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.


Solution

  • 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).