I'm getting jQuery requires a window with a document
error and it seems that I need an answer like: Error: jQuery requires a window with a document
What I'm looking for is the correct syntax for this using import
rather than require
because I'm not require
-ing anything else in my Node app. I'm trying to get this to work in a React component. I tried:
import jsdom from 'jsdom';
import $ from 'jquery';
jsdom.jsdom().createWindow();
and:
import jsdom from 'jsdom';
const $ = require('jquery')(jsdom.jsdom().createWindow());
But that's not getting me there. How do I resolve this?
NOTE : This only works for older versions of jsdom.
I do this for the tests for using jQuery Mockjax in a Node context . Essentially, I use the jsdom env()
method to generate the window object with a blank HTML document:
const jsdom = require('jsdom');
let $;
jsdom.env('<html></html>', function (error, window) {
if (error) {
// handle this somehow
} else {
$ = require('jquery')(window);
// now you have it...
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.