2

I am trying to build a simple IRC bot for Twitch in Node.js with this framework https://github.com/Schmoopiie/twitch-irc that can connect to the API and check certain things, in this reference I will be checking the followers on the current channel the bot is located in. Except I am running into an error, I keep getting this error logged below.

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

This is the code that I am using to run, in order to check the 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);
        });
    }

});

I have been attempting to get this refferenceerror to go away for hours, but I am just not familiar enough with using JS and JQ outside of the browser environment to produce any results, any insight would be much appreciated.

6
  • check if you have jquery library loaded properly. Commented Jan 11, 2015 at 9:38
  • I can see it in the node_modules, not sure if there is a command line code I could run to check. puu.sh/emxxZ/f0d0b4a9c1.png Commented Jan 11, 2015 at 9:42
  • 1
    $ is not global and it's not defined inside the client.addListener callback. Commented Jan 11, 2015 at 9:44
  • var jQuery = require('jQuery'); Would you suggest adding this inside of the client.addListener, I added it at the top of the script just like the others Commented Jan 11, 2015 at 9:47
  • 2
    When you say var jQuery = require('jQuery') you're assigning jQuery to a variable called jQuery. If you want to use $ instead you'll have to do var $ = require('jQuery'). Commented Jan 11, 2015 at 9:47

2 Answers 2

5

Be sure to use the $ symbol equals require('jquery') inside each module you will be using jQuery.

The proper syntax is:

var $ = require("jquery");

For more information visit package.

Sign up to request clarification or add additional context in comments.

Comments

3

Juhana Jan provided the correct answer in the comments to the original question:

When you say var jQuery = require('jQuery') you're assigning jQuery to a variable called jQuery. If you want to use $ instead you'll have to do var $ = require('jQuery'). – Juhana Jan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.