Nodejs jQuery needs jsdom
Asked Answered
L

5

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

Lazybones answered 15/5, 2016 at 14:47 Comment(2)
Is there a reason why want to use jquery to request json data in nodejs?Ikhnaton
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.Brest
O
13

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());
Ordinance answered 6/5, 2017 at 12:11 Comment(1)
Perfect! All of the code examples I was finding were for the outdated JSDOM syntax. Thanks so much! :)Stetson
I
8

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

Inheritrix answered 1/11, 2016 at 0:6 Comment(1)
that does not help.Coeliac
F
3

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.

Furry answered 10/11, 2017 at 3:41 Comment(0)
A
0

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

Acerbity answered 15/5, 2016 at 15:28 Comment(4)
is jsdom in the node_modules directory, or anywhere else where it can be loaded with 'require' ?Acerbity
it's in node_modulesLazybones
remove line 5. It's still including jQuery the wrong way.Acerbity
This is getting a bit hard to debug. Is it possible that you are using an quite old version of jQuery, that's still expecting an older version of jsdom?Acerbity
N
0

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();
Niveous answered 3/5, 2017 at 16:13 Comment(1)
still not work for me : TypeError: $.getJSON is not a functionLibreville

© 2022 - 2025 — McMap. All rights reserved.