OrchardCMS/OrchardCore · error · Error
jQuery requires a window with a document
Error message
jQuery requires a window with a document
What it means
jQuery's UMD/CommonJS wrapper throws this when the library is loaded in a CommonJS environment (module.exports exists) but the supplied window/global object has no document property. jQuery needs a real DOM document at factory time to initialize (e.g. attach events, feature-detect the DOM). jQuery 3.x cannot run in a pure non-DOM environment without a window shim.
Solutions
- In Node/tests, provide a DOM: global document via jsdom — require('jsdom') and pass { window } or set global.window/global.document before requiring jQuery.
- Don't import jQuery in server-side code; guard the import or use dynamic import only on the client.
- Use a jQuery version/build intended for the environment, or replace the jQuery usage with plain DOM/framework code in non-browser contexts.
- If passing a custom window to the factory, ensure it exposes a real document object.
Example fix
// before (Node)
const $ = require('jquery'); // throws: no document
// after
const { JSDOM } = require('jsdom');
const { window } = new JSDOM('<!DOCTYPE html>');
global.window = window;
global.document = window.document;
const $ = require('jquery')(window); Defensive patterns
Strategy: validation
Validate before calling
// Guard before requiring/using jQuery in Node
if (typeof document === 'undefined' && typeof global.window === 'undefined') {
throw new Error('jQuery requires a DOM environment; use jsdom or skip on the server');
}
const $ = require('jquery')(global.window); Type guard
function hasDom(win) { return !!win && !!win.document && typeof win.document.createElement === 'function'; } Try / catch
let $;
try { $ = require('jquery')(window); }
catch (e) {
if (/window with a document/.test(e.message)) {
const { JSDOM } = require('jsdom');
const dom = new JSDOM('');
$ = require('jquery')(dom.window);
} else throw e;
} Prevention
- Never import the browser jQuery build in Node, SSR, or Web Worker code paths.
- Set up jsdom (global.window/document) in test harnesses before any module requires jQuery.
- Isolate jQuery-dependent code behind dynamic import guarded by typeof window !== 'undefined'.
- Ensure any custom window object passed to the jQuery factory exposes a real document.
When it happens
Trigger: Requiring jquery in Node.js without jsdom or any DOM shim: require('jquery') with global.document undefined; passing an object without .document to the factory: require('jquery')(fakeWindow) where fakeWindow.document is missing; loading the browser build (jquery.slim.js) in a worker/SSR/server context.
Common situations: Server-side rendering (SSR) of an app that imports jQuery; running jQuery-dependent code in unit tests under Node without jsdom; Web Workers importing jQuery; misconfigured bundlers resolving the wrong build (browser build pulled into a server bundle).
Related errors
- jQuery requires a window with a document
- jQuery requires a window with a document
- jQuery requires a window with a document
- jQuery requires a window with a document
- jQuery requires a window with a document
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/293cabecebe214ce.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore.Modules/OrchardCore.Resources/wwwroot/Vendor/jquery-3.6.0/jquery.slim.js:31
*/
( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
// Pass this if window is not defined yet
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
// Edge <= 12 - 13+, Firefox <=18 - 45+, IE 10 - 11, Safari 5.1 - 9+, iOS 6 - 9.1
// throw exceptions when non-strict code (e.g., ASP.NET 4.5) accesses strict mode
// arguments.callee.caller (trac-13335). But as of jQuery 3.0 (2016), strict mode should be common
// enough that all such attempts are guarded in a try block.
"use strict";
var arr = [];
View on GitHub (pinned to 4306c0717f)