Node.js & JQuery: “ReferenceError: $ is not defined” error. How do I use jquery with node on the server?

后端 未结 4 1562
太阳男子
太阳男子 2021-01-21 02:21

Help! I\'m trying to use jquery in my node.js app, but I keep getting an error when I try to use \'$\', saying \"$ is not defined\"... but I defined it at the top! Here\'s what

4条回答
  •  爱一瞬间的悲伤
    2021-01-21 02:51

    //make sure $ is available in the global scope
    var $;
    
    function doSomething(){
        var deferred = $.Deferred();
    }
    
    require("jsdom").env("", function(err, window) {
        if (err) {
            console.error(err);
            return;
        }
        // assign it
        $ = require("jquery")(window);
    
        // you have to call it in here because you are in a callback 
        doSomething();
    });
    

提交回复
热议问题