OrchardCMS/OrchardCore · error · Error

jQuery requires a window with a document

Error message

jQuery requires a window with a document

What it means

Same throw as in jquery.js: when the slim bundle runs under CommonJS without a global document, the exported factory requires a window argument whose document exists; otherwise it throws 'jQuery requires a window with a document'. jQuery slim lacks ajax and effects but has the same DOM-environment requirement.

Solutions

  1. Run slim jQuery only in a browser environment with a document
  2. Provide a jsdom window in Node: require('jquery-slim')(window) — or use the full package with require('jquery')(window)
  3. Mark jquery as browser-only in your bundler and avoid importing it during SSR
  4. Load the script with the normal <script> tag so it uses the browser global

Example fix

// before (worker/SSR)
const $ = require('./jquery.slim.js')({});
// after
const { JSDOM } = require('jsdom');
const $ = require('./jquery.slim.js')(new JSDOM('').window);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof window === 'undefined' || !window.document) { throw new Error('jquery.slim.js requires a browser window'); }

Type guard

function hasDocument(w) { return !!w && typeof w.document !== 'undefined' && w.document !== null; }

Try / catch

try { const $ = require('./jquery.slim.js')(); } catch (e) { if (e.message.includes('window with a document')) { // run only on client or provide jsdom } }

Prevention

When it happens

Trigger: Loading jquery.slim.js in Node, a web worker, or SSR context; calling the module-exported factory with an object lacking .document; bundlers evaluating the file outside a browser.

Common situations: Server-rendered pages importing slim jQuery; Node test runners without jsdom; script inlining tools executing the file at build time.

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/354f9d9b44e0d738. Report an issue: GitHub.

Appendix: source

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