javascript - 将 JQuery 与 Node.js 一起使用 "ReferenceError: $ is not defined"

标签 javascript jquery node.js referenceerror

我正在尝试使用此框架在 Node.js 中为 Twitch 构建一个简单的 IRC 机器人 https://github.com/Schmoopiie/twitch-irc可以连接到 API 并检查某些内容,在此引用中,我将检查机器人所在的当前 channel 上的关注者。除了我遇到错误外,我一直在下面记录此错误。

[31mcrash[39m: ReferenceError: $ is not defined
    at client.<anonymous> (C:\Users\Exuviax\Desktop\node.js\bot.js:157:9)
    at client.emit (events.js:117:20)
    at C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\client.js:657:30
    at Object.createChannelUserData (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\data.js:56:2)
    at client._handleMessage (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\library\client.js:651:22)
    at Stream.emit (events.js:95:17)
    at drain (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\node_modules\through\index.js:36:16)
    at Stream.stream.queue.stream.push (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\node_modules\through\index.js:45:5)
    at LineStream.<anonymous> (C:\Users\Exuviax\Desktop\node.js\node_modules\twitch-irc\node_modules\irc-message-stream\index.js:22:16)
    at LineStream.emit (events.js:95:17)

这是我用来运行的代码,以检查 API

var irc = require('twitch-irc');
var colors = require('colors');
var jQuery = require('jQuery')
var jsdom = require("jsdom");
var window = jsdom.jsdom().parentWindow;

jsdom.jQueryify(window, "http://code.jquery.com/jquery-2.1.3.min.js", function () {
  var $ = window.$;
  $("body").prepend("<h1>The title</h1>");
  console.log($("h1").html());
});

// Calling a new client..
var client = new irc.client({
    options: {
        debug: true,
        debugIgnore: ['ping', 'chat', 'action'],
        logging: true,
        tc: 3
    },
    identity: {
        username: 'BotName',
        password: 'oauth:Code'
    },
    channels: ['#my', '#first', '#connect', '#channels']
});

// Connect the client to server..
client.connect();

client.addListener('chat', function(channel, user, message) {
    if (message.indexOf('!followers') === 0) {
        var channels = channel
        channels = channels.replace('#', '');
        $.getJSON('https://api.twitch.tv/kraken/channels/' + channels + '.json?callback=?', function(data) {
            var followers = data.followers
            client.say(channel, "Current Followers: " + followers);
            console.log("Followers for " + channel + " " + followers);
        });
    }

});

几个小时以来,我一直试图让这个引用错误消失,但我对在浏览器环境之外使用 JS 和 JQ 来产生任何结果还不够熟悉,如果有任何见解,我们将不胜感激。

最佳答案

请务必使用 $ 符号等于 require('jquery') 在您将使用 jQuery 的每个模块中

正确的语法是:

var $ = require("jquery");

欲了解更多信息,请访问 package .

关于javascript - 将 JQuery 与 Node.js 一起使用 "ReferenceError: $ is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27885670/

相关文章:

javascript - 如何使用 f :form. select 和 onclick:

javascript - 当我单击标记时,不会出现弹出窗口。我收到以下错误

javascript - 如何通过 `echo json_encode($data);`获取所有数据

node.js - 如何将自定义连接器集成到 Botium Box

javascript - 从输入传递 localStorage 数据

javascript - 迭代数值并作为 Prop 发送

jquery - .each() 跳过第一项

jquery - 如何删除div中的p标签?

javascript - 如何在 ExpressJS 中处理全局数据

javascript - 是否可以使用 node.js 模块编写 Chrome 应用程序?