Initializing jquery from Node

I am creating a new project using the following:

$mkdir X $cd X $npm install jquery 

Then create a new app.js file:

 var http = require('http'); var $ = require('jquery'); console.log("http="+ http); console.log("$="+ $); console.log("$.getJSON="+ $.getJSON); 

Exit:

 http=[object Object] $=function ( w ) {...} $.getJSON=undefined 

Why is $ .getJSON undefined? Using the latest version of io.js v2.4.0.

+1
jquery init
Jul 27 '15 at 13:51 on
source share
2 answers

You are trying to create XHR from Node.js. This will not work, as Node.js is just a JavaScript runtime and does not match the browser.

If you want to get something from the HTTP protocol, you can use something like request . For example (from official documents):

 var request = require('request'); request('http://www.google.com', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Show the HTML for the Google homepage. } }) 

You can see this answer (also from me) for more information on using jQuery in conjunction with Node.js.

UPDATE [d again!]:

So, you wanted to know how the jQuery node module differs between the browser and the node environment? When you require jQuery inside CommonJS or a similar environment that provide module and module.exports , you get a factory, not the actual jQuery object. As you can see below, factory can be used to create a jQuery object, i.e. using jsdom :

 let jsdom = require("jsdom"); let $ = null; jsdom.env( "http://quaintous.com/2015/07/31/jquery-node-mystery/", function (err, window) { $ = require('jQuery')(window); } ); 

Here's how jQuery distinguishes between browser and io.js (or Node.js):

 (function( global, factory ) { if ( typeof module === "object" && typeof module.exports === "object" ) { // For CommonJS and CommonJS-like environments where a proper `window` // is present, execute the factory and get jQuery. // For environments that do not have a `window` with a `document` // (such as Node.js), expose a factory as module.exports. // This accentuates the need for the creation of a real `window`. // eg var jQuery = require("jquery")(window); // See ticket #14549 for more info. module.exports = global.document ? factory( global, true ) : function( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); }; } else { factory( global ); } // Pass this if window is not defined yet }(typeof window !== "undefined" ? window : this, function( window, noGlobal ) { // implementation return jQuery; })); 

I would use the jQuery npm package for custom builds instead of being used with require !

UPDATE

I had the feeling that this question usually made some developers busy, so I combined a couple of my own answers and wrote an article about the whole jQuery / Node combination!

+2
Jul 27 '15 at 14:13
source share

If you want to load jquery synchronously, you can use something like the following

 var jsdom = require('jsdom'); var jQuery = require('jquery')(jsdom.jsdom('<p><a>jsdom!</a></p>').defaultView); 
0
May 12 '16 at 4:49
source share



All Articles