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)
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" ) {
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!
Yan Foto Jul 27 '15 at 14:13 2015-07-27 14:13
source share