How to use npm jquery module

0 votes

How should I requirethe jquery in node if I use it in multiple modules? Should I define it as a global or should I just use the require('jquery)` in every module I need it?

I am getting an error when trying to use the package.

TypeError: Object function ( w ) {
            if ( !w.document ) {
                throw new Error( "jQuery requires a window with a document" );
            }
            return factory( w );
        } has no method 'isArray'

It looks like a bug in the current release as it should not check if I am running it in a browser according to the official documentation. This issue is also mentioned in another post. It works with version 1.8.3 as mentioned in one of the answers.

Jun 21, 2022 in JQuery by gaurav
• 23,260 points
3,227 views

1 answer to this question.

0 votes

To use jquery in node, you need to have two separate node package installations.

  1. jquery
  2. jsdom to create a dummy window object which jquery can use.

Installation:

npm install jquery
npm install jsdom

In code:

var jsdom = require("jsdom").jsdom;
global.$ = require('jquery/dist/jquery')(jsdom().createWindow());
answered Jun 21, 2022 by rajatha
• 7,680 points

Related Questions In JQuery

0 votes
1 answer

How to use $(document).ready equivalent without jQuery

Hello @kartik, Using DOMContentLoaded that is supported by over ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,880 points
1,217 views
0 votes
1 answer

How to make $.post() use contentType=application/json in jquery?

Hii, Try this: $.ajax({ url:url, type:"POST", ...READ MORE

answered Nov 24, 2020 in JQuery by Niroj
• 82,880 points
9,932 views
0 votes
1 answer

jQuery.inArray(), how to use it right?

The jQuery inArray() method is used to find ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,670 points
341 views
0 votes
1 answer

How to import jQuery UI using ES6/ES7 syntax?

Hii, Add a alias in webpack config: resolve: { ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,880 points
7,430 views
0 votes
0 answers

How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)

My node version is: node -v v0.6.1-pre Then I install ...READ MORE

May 16, 2022 in Java-Script by Kichu
• 19,050 points
974 views
0 votes
1 answer

Nodejs Instagram videos downloader not working

Firstly, Initialize a package.json file with default values. npm ...READ MORE

answered Jun 9, 2022 in Node-js by Neha
• 9,060 points
989 views
0 votes
1 answer

jQuery.inArray(), how to use it right?

inArray returns the index of the element in ...READ MORE

answered Jun 21, 2022 in JQuery by rajatha
• 7,680 points
1,390 views
0 votes
1 answer

How to open a Bootstrap 4 modal dialog using jQuery

Use the modal('show') Method You can simply use ...READ MORE

answered Jun 28, 2022 in JQuery by rajatha
• 7,680 points
6,576 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP