TO USE JQUERY IN NPM AND EXPRESS :
You need to first download the jquery package
npm install jquery
For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as jsdom. This can be useful for testing purposes.
So you must now add the code snippet to your app.js:
const { JSDOM } = require( "jsdom" );
const { window } = new JSDOM( "" );
const $ = require( "jquery" )( window );
Also you must install jsdom package:
npm i jsdom
Since require is not defined in ES module scope, this file is being treated as an ES module because it has a '.js' file extension.
So you must import the Nodejs module : "module" and then create a require function by call createRequire.
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
Add this code before using require.
Now create a separate .js file to store your jquery file that contains your program.
Now you must mount the static route to the directory of the .js file that contains your jquery program.
app.use(express.static("directory"));
Now in the (index.ejs) or your .ejs file, you need to add the script tag to link the .js file. Also to use jquery you must add its CDN to the program.
Add the line of codes just before the closing body tag.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<script src="query.js" charset="utf-8" type="text/javascript"></script>
Hope this helps you!!!