6
votes
$.getJSON('https://api.twitch.tv/kraken/channels/' + SLoppierKitty7, function(channel) {

if (channel["stream"] == null) { 
    var live ="no"

} else {

      var live ="yes"

}

that is my code but when i run it i get the following error

E:\Sloppers bot\node_modules\jQuery\lib\node-jquery.js:5 window = require('jsdom').jsdom().createWindow(); ^

TypeError: require(...).jsdom(...).createWindow is not a function at create (E:\Sloppers bot\node_modules\jQuery\lib\node-jquery.js:5:39) at E:\Sloppers bot\node_modules\jQuery\lib\node-jquery.js:9435:18 at Object. (E:\Sloppers bot\node_modules\jQuery\lib\node-jquery.js:9437:2) at Module._compile (module.js:434:26) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at Object. (E:\Sloppers bot\bot.js:2:9)

what do i do

this is for a bot i'm working

5
Is there a reason why want to use jquery to request json data in nodejs? - t.niese
Unless you have a need for jquery beyond $.getJSON, I would suggest to use npmjs.com/package/request which is a simpler wrapper around Node's native HTTP module. - tmslnz

5 Answers

13
votes

jquery 3.2.1, jsdom 10.1.0. It works.

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(`<!DOCTYPE html>`);
const $ = require('jQuery')(window);

$('<h1>Hello</h1>').appendTo('body');
console.log($('h1').text());
8
votes

I encountered the same problem, and it is OK now after I changed the package from require('jQuery') to require('jquery'). It seems that the later package (jquery) use a more recent version of jQuery than the former one (jQuery).

3
votes

I faced the same issue, and while debugging I came to know about something new.

When you do -

npm install jQuery

It installs the jQuery version 1.7.4 to your project.

And when you do -

npm install jquery

It installs the jQuery version 3.2.1 to your project.

enter image description here

The difference between both the command is just the uppercase Q. So if you are getting this error then you are probably using the old version of jQuery.

You can read more about the first command (the old version) here and about the second command here.

0
votes

Yes, it does. jQuery expects the 'window' to be there, which is normally a browser-only object. As such, the window needs to be simulated. That is what jsdom does.

After including jsdom (npm install jsdom):

// Load jsdom, and create a window.
var jsdom = require("jsdom").jsdom;
var doc = jsdom();
var window = doc.defaultView;

// Load jQuery with the simulated jsdom window.
$ = require('jquery')(window);

See the jsdom documentation for more information: https://www.npmjs.com/package/jsdom

0
votes

nothing worked for me from here,

my jquery version:3.2.1

jsdom version: 10.1.0 found a solution finally:

var myHtmlString = 'akkadsf lakuseh alhf lasudfa ls<p></p>';

function jQuery(doc){
    const jsdom = require("jsdom");
    const { JSDOM } = jsdom;
    const { window } = new JSDOM();
    const JQ = require("jquery")(window)(`<html>${doc || ''}</html>`);
}
var $ = jQuery;
// my html string:
var text = $(myHtmlString).text();