OrchardCMS/OrchardCore · error · Error

jQuery requires a window with a document

Error message

jQuery requires a window with a document

What it means

jQuery is being loaded in a module (CommonJS) environment where no global 'window' with a 'document' exists. The UMD wrapper detects the missing document and throws this error instead of silently producing a broken jQuery instance. jQuery is inherently browser-DOM-bound; it cannot initialize without a window object.

Solutions

  1. In Node/test environments, set up a DOM before requiring jQuery: use jsdom and pass its window, e.g. require('jquery')(dom.window) (install jsdom first).
  2. Do not import the vendored wwwroot copy server-side; import the official 'jquery' npm package which handles module environments.
  3. If loading in a worker/vm, provide a fake window object with a document stub, or avoid jQuery there and use DOM-free APIs.
  4. In Jest, enable testEnvironment: 'jsdom' (default in jsdom-based setups) so window/document exist before jQuery loads.

Example fix

// before (Node)
const $ = require('./wwwroot/Vendor/jquery-3.5.1/jquery.js'); // throws
// after
const { JSDOM } = require('jsdom');
const $ = require('jquery')(new JSDOM('').window);
Defensive patterns

Strategy: validation

Validate before calling

// Before loading jQuery in a module context:
if (typeof window === 'undefined' || !window.document) {
  // skip jQuery load, use jsdom, or load a DOM-free fallback
  const { JSDOM } = require('jsdom');
  global.window = new JSDOM('').window;
  global.document = global.window.document;
}

Type guard

function hasDom(win) { return !!win && !!win.document && typeof win.document.createElement === 'function'; }

Try / catch

try { const $ = require('jquery'); } catch (e) { if (e.message.includes('window with a document')) { initJsdomThenRetry(); } else { throw e; } }

Prevention

When it happens

Trigger: Requiring/importing jquery.js under Node.js, a headless/vm sandbox, a web worker, or any CommonJS loader where 'global.document' is undefined and the returned factory function is invoked with an object lacking a 'document' property.

Common situations: Server-side rendering or unit tests (Jest/Node) importing the browser build directly from wwwroot; running the vendored file inside a worker; forgetting jsdom/window polyfill in test setup; accidentally bundling the IIFE browser file with a CJS bundler.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/f449a6bbc021f412. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.Resources/wwwroot/Vendor/jquery-3.5.1/jquery.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)