Node.JS: How to check if jQuery has loaded

I used npm install jquery, and I put this bit of code in my server.js to check if jQuery exists:

io.sockets.on('connection', function(socket) {

    console.log("Connection " + socket.id + " accepted.");

    // Check if jQuery is loaded
    if (jQuery) {  
        console.log("jQuery loaded!");
    }
    if (typeof jQuery == 'undefined') {  
        console.log('jQuery has not been loaded!');  
    }

});

When I run server.js, I get ReferenceError: jQuery is not defined.

Is there a better way to check if jQuery is loaded, or does this message mean that I have not successfully installed jQuery?

EDIT:

client.html

<body>

<script>
    function send() {
        var user_message = $("#message_box").val();
        socket.send(user_message);
    };
</script>

<input id="message_box" type="text" placeholder="Message" />
<button id="sender" onClick='send()'/>Send Message</button>

</body>

That's not web borwser, you should require jQuery module and define jQuery variable yourself (just like you do with SocketIO).

var jQuery = require('jQuery')

Checkout docs for details.


upd

Regarding question update.

You do not need to npm install jquery for making library work in your HTML pages (since javascript in those will be executed in user's browser, and not Node). What you need to do is include remote script in your HTML-documet like this:

<html>
  <head>
    <!-- Embedd jQuery into HTML page to make it available in browser -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
      // So, browser now knows about jQuery, but don't know about anything in <body>
      // We should wait, untill page will be fully loaded, and only then
      // read html input values, initiate connections, etc.
      $(function() {
        // this code will be executed after DOM is ready
        send();
      });

      // here is your send function
      function send() {
        var user_message = $("#message_box").val();
        socket.send(user_message);
      };
    </script>

  </head>
<!-- ... -->

IMO, you should really go through some tutorials on client-side javascript programming (with or without jQuery — doesn't really matter). Then, see some tutorials about working with Node as a backend for your site.

You are looking to see if the variable JQuery is there, which in your code, you haven't defined the variable JQuery. JQuery isn't a variable, therefore it won't work.

You could try checking for window.jQuery, the window should exist so you should get undefined back instead.

I would actually set something else up though maybe something like

$(function () {
  Console.log( 'jQuery loaded');
});

Then you can respond to that on your server however you need to.

I'm not hugely familiar with socket.io so I suspect there may be a better way, but this would at least give you the info you need.

Using typeof jQuery == 'undefined' will not crash if jQuery is undefined, so you could just use something like this :

if (typeof jQuery == 'undefined') {  
    console.log('jQuery has not been loaded!');  
}
else {
    console.log('jQuery loaded!');
}

If I understand the question correctly, you're just trying to make sure you're not sending socket.io data to the web browser until after jQuery is loaded and ready on the browser correct? If that is the case, then all you have to do is add the script tag referencing jQuery like normal in the html, and then initialize the socket.io client in $(document).ready. Something like the following:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="socket.io/socket.io.js"></script>
<script>
    $(document).ready(function() {
        var socket = io.connect('http://localhost');
        socket.on('news', function (data) {
            $('#news').append(data);
        });
    });
</script>