Basic error when using jquery with nodejs

后端 未结 4 1519
情话喂你
情话喂你 2021-01-14 07:01

I\'m trying to use some jquery in my project and as soon as I tried using it I came across an error in copied code and can\'t get any google help on it

 var          


        
相关标签:
4条回答
  • 2021-01-14 07:10

    You can't use jquery on server as it explicitly requires window and window.document. Try cheerio

    0 讨论(0)
  • 2021-01-14 07:17

    try:

    var jsdom = require("jsdom").jsdom;
    var markup = '<html><body><h1 class="example">Hello World!</h1><p class="hello">Heya Big World!</body></html>';
    var doc = jsdom(markup);
    var window = doc.parentWindow;
    var $ = require('jquery')(window)
    console.log($('.example').text());
    

    Basically you can use the jsdom module to create a DOM out of your markup and then you can use it as you would with jquery in the browser.

    0 讨论(0)
  • 2021-01-14 07:25

    I had the same problem. To solve it I installed an older version of jquery: npm install jquery@1.8.3

    0 讨论(0)
  • 2021-01-14 07:26

    It used to work just fine for many months. But then they changed something and now it doesn't. I could run jquery without a window just fine and it worked perfectly on node js. But not with the latest version for some reason. Really weird.

    What we are trying to do is just this:

    $ = require("jquery"); $("..).find(".message").html("Hello World!");

    0 讨论(0)
提交回复
热议问题