SloppierKitty7
SloppierKitty7

Reputation: 81

Nodejs jQuery needs jsdom

$.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

Upvotes: 6

Views: 14841

Answers (5)

Rito
Rito

Reputation: 3290

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.

Upvotes: 3

sutara79
sutara79

Reputation: 171

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());

Upvotes: 13

Raza Ahmed
Raza Ahmed

Reputation: 2751

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();

Upvotes: 0

jinreal
jinreal

Reputation: 1555

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

Upvotes: 8

TinkerTank
TinkerTank

Reputation: 5805

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

Upvotes: 0

Related Questions