How to Use jQuery with Node.js ?
Last Updated :
14 Jun, 2024
jQuery is a popular JavaScript library primarily used for client-side scripting to simplify HTML document traversal, event handling, animation, and AJAX interactions. Although jQuery is designed for the browser, you might find scenarios where you want to use it on the server side with Node.js, such as for web scraping or manipulating HTML documents. In this article, we'll explore how to use jQuery with Node.js.
Note: Use the 'jquery' module, not the 'jQuery' module as the latter is deprecated.
Getting jQuery to work in NodeJS
Step 1 :
Making the package.json file. Use the following command to create the package.json file, which keeps track of the modules and dependencies.
npm init -y
The '-y' tag makes yes the default answer for all the questions asked while creating the package.json file.
Step 2:
Installing the jquery module. Use the following command to install the jquery module.
npm install jquery
Step 3 :
Installing the jsdom module. Since jQuery is a frontend JavaScript Library, it needs to have a window with a document to work in the backend. 'jsdom' is a library that is used to parse and interact with the HTML. It is not exactly a web browser but mimics one. Use the following command to install the jsdom module.
npm install jsdom
Step 4 :
Importing the jsdom module. Use the require method to import the jsdom module.
const jsdom = require('jsdom')
Step 5:
Creating a new window. We create a window with a document by creating a JSDOM object, with the HTML code as the parameter. Following code is used to create a window with a document :-
const dom = new jsdom.JSDOM("")
Step 6:
Importing jQuery and providing it with a window. Once the window with a document is created, we can use the jquery module by providing it with the window we created. The following code is used to import the jquery module.
const jquery = require('jquery')(dom.window)
That's it we have successfully loaded jquery into our Node.js application.
Example: To get a better understanding of how it works, please go through the below example :-
JavaScript
// Importing the jsdom module
const jsdom = require("jsdom");
// Creating a window with a document
const dom = new jsdom.JSDOM(`<!DOCTYPE html>
<body>
<h1 class="heading">
GeeksforGeeks
</h1>
</body>
`);
// Importing the jquery and providing it
// with the window
const jquery = require("jquery")(dom.window);
// Appending a paragraph tag to the body
jquery("body").append("<p>Is a cool Website</p>");
// Getting the content of the body
const content = dom.window.document.querySelector("body");
// Printing the content of the heading and paragraph
console.log(content.textContent);
Output:
GeeksforGeeks
Is a cool website
Conclusion
By using jsdom
, you can leverage the power of jQuery in a Node.js environment. This setup is particularly useful for tasks such as web scraping, where you need to interact with HTML content on the server side. With this knowledge, you can now integrate jQuery into your Node.js projects and take advantage of its powerful features for DOM manipulation and traversal.
Similar Reads
How to replace jQuery with Vue.js ?
In this article, we will see how to replace jQuery with Vue. jQuery: jQuery is a fast and rich JavaScript library. It is an application programming interface (API), a cross-platform JavaScript library designed to improve web browser features. It makes things easier like works across HTML document tr
6 min read
How to use OpenCV with Node.js?
OpenCV is a free-to-use cross-platform programming library that is mainly utilized for computer vision. Â It is written in C/C++. Computer vision is widely used in applications like face detection, autonomous vehicles, etc. To use this with Node.js we will use opencv4nodejs library. Opencv4nodejs all
3 min read
How to use jQuery with TypeScript ?
In this article, we will learn how we can use jQuery with TypeScript and implement the features of both languages. The below approach can be used to implement jQuery in TypeScript. By installing jQuery using the npm commandThe jQuery can be installed in your current TypeScript project folder using t
2 min read
How to use JSON in Ajax jQuery ?
Using JSON in AJAX requests with jQuery is a fundamental aspect of web development. JSON or JavaScript Object Notation, offers a lightweight and structured format for data exchange between a server and a web application. jQuery simplifies this process further through its AJAX functionalities. We wil
3 min read
How to use Node.js REPL ?
Node.Js REPL or Read-Evaluate-Print Loop is an interactive shell for the Node.js environment which means we can write any valid Javascript code in it. This is used to test, evaluate, experiment, or debug code much easier and accessible way. It basically acts as the Browser's Web dev tools' Console f
6 min read
How to Use MongoDB and Mongoose with Node.js ?
MongoDB is a popular NoSQL database that offers flexibility and scalability, making it an excellent choice for modern applications. Mongoose, a powerful ODM (Object Data Modeling) library, simplifies the interaction between MongoDB and Node.js by providing a schema-based solution for data validation
6 min read
How To Use Node Modules with npm and package.json
NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This
3 min read
How to work with Node.js and JSON file ?
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. With node.js as backend, the developer can maintain a single codebase for an entire application in javaScript.JSON on the other hand, stands for JavaScript Object Notation. It is a lightweight format for storing and exchanging d
4 min read
Use of CORS in Node.js
The word CORS stands for "Cross-Origin Resource Sharing". Cross-Origin Resource Sharing is an HTTP-header based mechanism implemented by the browser which allows a server or an API(Application Programming Interface) to indicate any origins (different in terms of protocol, hostname, or port) other th
4 min read
What is NPM & How to use it ?
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integra
3 min read