OrchardCMS/OrchardCore · error · Error

jQuery requires a window with a document

Error message

jQuery requires a window with a document

What it means

Identical to the jquery.js case: the UMD wrapper of the slim build detects a module environment where no 'window' with a 'document' exists and throws. The slim build only omits ajax/effects/animations; it still requires a full DOM to initialize.

Solutions

  1. In Node/tests, provide a DOM first: const $ = require('jquery')(new JSDOM('').window) using the official npm package, not the vendored file.
  2. Only load jquery.slim.js in real browser window contexts; for workers use DOM-free alternatives.
  3. If the environment legitimately has no DOM, guard the import (dynamic require inside a browser-only branch) so it never executes server-side.
  4. In Jest/jsdom setups, ensure testEnvironment provides window/document before the module is imported.

Example fix

// before
const $ = require('./wwwroot/Vendor/jquery-3.5.1/jquery.slim.js'); // throws in Node
// after
if (typeof window !== 'undefined' && window.document) {
  var $ = require('jquery');
}
Defensive patterns

Strategy: validation

Validate before calling

if (typeof window === 'undefined' || !window.document) {
  throw new Error('jquery.slim.js cannot load: no window.document available');
}
var $ = require('jquery');

Type guard

function canLoadJQuery() { return typeof window !== 'undefined' && typeof window.document !== 'undefined'; }

Try / catch

try { $ = require('./Vendor/jquery-3.5.1/jquery.slim.js'); } catch (e) { if (/window with a document/.test(e.message)) { $ = null; /* headless path */ } else { throw e; } }

Prevention

When it happens

Trigger: Requiring jquery.slim.js under Node.js, in a web worker, or inside a vm/sandbox with no global document; invoking the exported factory with an object whose 'document' property is missing.

Common situations: SSR or unit tests importing the vendored slim build from wwwroot; bundlers picking jquery.slim.js in a Node target; switching a page from jquery.js to jquery.slim.js in an environment (worker/extension context) that lacks a DOM.

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/95a376ba9fe6b316. Report an issue: GitHub.

Appendix: source

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