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 in JQuery by gaurav
• 17,760 points
58 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 by rajatha
• 7,040 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,740 points
276 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,740 points
3,024 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 in JQuery by Edureka
• 13,640 points
33 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,740 points
5,218 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 in Java-Script by Kichu
• 19,040 points
152 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 in Node-js by Neha
• 8,740 points
83 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 in JQuery by rajatha
• 7,040 points
33 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 in JQuery by rajatha
• 7,040 points
113 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP