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

  1. In Node/tests, provide a DOM: global document via jsdom — require('jsdom') and pass { window } or set global.window/global.document before requiring jQuery.
  2. Don't import jQuery in server-side code; guard the import or use dynamic import only on the client.
  3. Use a jQuery version/build intended for the environment, or replace the jQuery usage with plain DOM/framework code in non-browser contexts.
  4. 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

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


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)