1

I installed jquery with : npm install jquery and then tried to run:

var $ = require('jquery');
console.log($('<a href="">hkjhjkh</a>').text());

I get this error, that I understand very well (no document in node.js!) :

Error( "jQuery requires a window with a document" )

How to make jQuery work without a document / window in node.js in such a simple case?


PS: I tried with: var $=require('jquery')(jsdom.jsdom().createWindow());, but unfortunately npm install jsdom gave me lots of make errors that I was unable to debug (that would be another question, so out of topic here).

Basj
  • 41,386
  • 99
  • 383
  • 673
  • Possible duplicate of http://stackoverflow.com/questions/21358015/error-jquery-requires-a-window-with-a-document – Amit Joki Dec 15 '14 at 11:15

1 Answers1

0

Try cheerio (https://github.com/cheeriojs/cheerio):

Fast, flexible, and lean implementation of core jQuery designed specifically for the server.

Your code would look like this:

var cheerio = require('cheerio'),
    $ = cheerio.load('<a href="">hkjhjkh</a>');

console.log($('a').text());