Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

like image 599
John Avatar asked Nov 26 '09 01:11

John


People also ask

Can you mix js and jQuery?

Yes, any code that you write in jQuery can also be written in vanilla JavaScript.

Is NodeJS and jQuery same?

NodeJs is an open-source framework based on JavaScript v8 engine. AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser.

Can AJAX be used in NodeJS?

Node, Ajax and JavaScript integration html file and refresh the web browser. Uploads to Node. js will go through Ajax, and thus create a full JavaScript file uploader with JavaScript running both on the client and the server.

Can I use JavaScript in NodeJS?

Node.js allows you to run JavaScript on the server.


1 Answers

Update (27-Jun-18): It looks like there was a major update to jsdom that causes the original answer to no longer work. I found this answer that explains how to use jsdom now. I've copied the relevant code below.

var jsdom = require("jsdom"); const { JSDOM } = jsdom; const { window } = new JSDOM(); const { document } = (new JSDOM('')).window; global.document = document;  var $ = jQuery = require('jquery')(window); 

Note: The original answer fails to mention that it you will need to install jsdom as well using npm install jsdom

Update (late 2013): The official jQuery team finally took over the management of the jquery package on npm:

npm install jquery 

Then:

require("jsdom").env("", function (err, window) {     if (err) {         console.error(err);         return;     }     var $ = require("jquery")(window); }); 

like image 148
Philippe Rathé Avatar answered Oct 12 '22 11:10

Philippe Rathé