{"record":{"id":"99d32dfa1cab9e8d","repo":"dotnet/AspNetCore.Docs","slug":"jquery-requires-a-window-with-a-document","errorCode":null,"errorMessage":"jQuery requires a window with a document","messagePattern":"jQuery requires a window with a document","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/jquery-2.2.0.js","lineNumber":29,"sourceCode":" *\n * Date: 2016-01-08T20:02Z\n */\n\n(function( global, factory ) {\n    \n        if ( typeof module === \"object\" && typeof module.exports === \"object\" ) {\n            // For CommonJS and CommonJS-like environments where a proper `window`\n            // is present, execute the factory and get jQuery.\n            // For environments that do not have a `window` with a `document`\n            // (such as Node.js), expose a factory as module.exports.\n            // This accentuates the need for the creation of a real `window`.\n            // e.g. var jQuery = require(\"jquery\")(window);\n            // See ticket #14549 for more info.\n            module.exports = global.document ?\n                factory( global, true ) :\n                function( w ) {\n                    if ( !w.document ) {\n                        throw new Error( \"jQuery requires a window with a document\" );\n                    }\n                    return factory( w );\n                };\n        } else {\n            factory( global );\n        }\n    \n    // Pass this if window is not defined yet\n    }(typeof window !== \"undefined\" ? window : this, function( window, noGlobal ) {\n    \n    // Support: Firefox 18+\n    // Can't be in strict mode, several libs including ASP.NET trace\n    // the stack via arguments.caller.callee and Firefox dies if\n    // you try to trace through \"use strict\" call chains. (#13335)\n    //\"use strict\";\n    var arr = [];\n    \n    var document = window.document;","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/jquery-2.2.0.js#L11-L47","documentation":"jQuery 2.2.0 ships a UMD wrapper. When loaded under CommonJS (module.exports) and there is no global.document, it exports a factory: `require('jquery')` returns a function expecting a window-like object. If you call that factory with an object lacking a `.document` property, jQuery refuses to initialize and throws. The error guards against a half-built DOM environment.","triggerScenarios":"`require('jquery')` in Node.js without jsdom, then calling the returned factory with {} or a non-window object; SSR frameworks that forget to pass jsdom's window; passing a detached Element instead of a window.","commonSituations":"Server-side rendering of jQuery-dependent code; unit-testing jQuery selectors in Node; Electron main process; migrating from jsdom to a lighter DOM shim that omits `document`.","solutions":["Provide a real window via jsdom: const { JSDOM } = require('jsdom'); const dom = new JSDOM(''); const $ = require('jquery')(dom.window);","If you only need jQuery's utilities (not DOM), consider cheerio instead, which is Node-native.","Ensure the object you pass has both `document` and `window` set."],"exampleFix":"// before (Node)\nconst $ = require('jquery')({}); // throws\n// after\nconst { JSDOM } = require('jsdom');\nconst dom = new JSDOM('<!DOCTYPE html>');\nconst $ = require('jquery')(dom.window);","handlingStrategy":"type-guard","validationCode":"// Before calling the jQuery factory in Node, ensure a window-like object.\nfunction getWindow() {\n  if (typeof window !== 'undefined') return window;\n  var jsdom = require('jsdom');\n  return new jsdom.JSDOM('<!DOCTYPE html>').window;\n}\nvar w = getWindow();\nif (!w || !w.document) {\n  throw new Error('No DOM environment available for jQuery');\n}\nvar $ = require('jquery')(w);","typeGuard":"function isWindowLike(w) {\n  return w != null && typeof w === 'object' && !!w.document && typeof w.document.createElement === 'function';\n}","tryCatchPattern":"try {\n  var $ = require('jquery')(providedWindow);\n} catch (e) {\n  if (/window with a document/.test(e.message)) {\n    var { JSDOM } = require('jsdom');\n    $ = require('jquery')(new JSDOM('').window);\n  } else { throw e; }\n}","preventionTips":["In Node, always pair jQuery with jsdom and pass dom.window.","For pure HTML parsing in Node prefer cheerio.","Never call the factory with an empty object or a DOM Element instead of a window."],"tags":["jquery","nodejs","commonjs","ssr","jsdom","environment"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}