Node.js & JQuery: "ReferenceError: $ is not defined" error. How do I use jquery with node on the server?
<p>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 I did:</p>

<p>I installed both packages from npm like so:</p>

<pre><code>npm install jquery
npm install jsdom
</code></pre>

<p>then I required them in my node.js app:</p>

<pre><code>require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }
    var $ = require("jquery")(window);
});
doSomething();
</code></pre>

<p>Then I'm trying to use it like so:</p>

<pre><code>function doSomething(){
    var deferred = $.Deferred();
}
</code></pre>

<p>and I get the following error:</p>

<pre><code>var deferred = $.Deferred();
               ^
ReferenceError: $ is not defined
</code></pre>

<p>Do you think that the <code>function</code> is getting executed <em>before</em> the <code>var $ =</code> part? </p>

<p>Thanks!</p>

<p>Versions:</p>

<ul>
<li>Node: 4.2.6</li>
<li>Express: 4.12.4</li>
<li>JQuery: 2.2.3</li>
<li>JSDom: 8.3.0</li>
</ul>

<h1>Update: Solution</h1>

<p>Here's what I ended up using, based on everyone's answers!</p>

<pre><code>var $;
require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }
    $ = require("jquery")(window);
    doSomething();
});
</code></pre>