Home >Web Front-end >JS Tutorial >How Can I Use jQuery with Node.js on the Server-Side?
Server-Side jQuery with Node.js
It may not be immediately apparent, but Node.js can actually be used to run jQuery selectors and DOM manipulation functions.
Dependencies
To get started, install the necessary dependencies using npm:
npm install jquery jsdom
Integration
Once installed, integrate jQuery with Node.js using the following steps:
var jsdom = require('jsdom'); const { JSDOM } = jsdom; const { window } = new JSDOM(); const { document } = (new JSDOM('')).window; global.document = document; var $ = jQuery = require('jquery')(window);
Usage
With jQuery now available in your Node.js environment, you can use it as you would on the client side. For instance, to select elements with a specific class:
$('body').find('.heading');
Note:
The jsdom dependency provides a JavaScript DOM implementation that is compatible with jQuery, allowing you to run jQuery code on the server.
The above is the detailed content of How Can I Use jQuery with Node.js on the Server-Side?. For more information, please follow other related articles on the PHP Chinese website!