Nodejs jQuery needs jsdom

一个人想着一个人 提交于 2019-12-01 02:13:45

问题


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


回答1:


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



回答2:


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:


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.

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.




回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/37239690/nodejs-jquery-needs-jsdom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!