How should I require
the 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.
js: We can use jQuery in Node. js using the jquery module. Note: Use the 'jquery' module not the 'jQuery' module as the latter is deprecated.
The name npm (Node Package Manager) stems from when npm first was created as a package manager for Node. js. All npm packages are defined in files called package.
To use jquery in node, you need to have two separate node package installations.
Installation:
npm install jquery npm install jsdom
In code:
var jsdom = require("jsdom").jsdom; global.$ = require('jquery/dist/jquery')(jsdom().createWindow());
Or, with newer versions of jsdom:
require("jsdom").env("", function(err, window) { if (err) { console.error(err); return; } var $ = require("jquery")(window); });
Using global.$ will make the jquery object($) available globally in your project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With