{"record":{"id":"79e8e7368763c6de","repo":"dotnet/AspNetCore.Docs","slug":"jquery-requires-a-window-with-a-document-79e8e7","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/views/tag-helpers/th-components/samples/RazorPagesSample/wwwroot/lib/jquery/jquery.js","lineNumber":29,"sourceCode":" *\n * Date: 2016-01-08T20:02Z\n */\n\n(function( global, factory ) {\n\n\tif ( typeof module === \"object\" && typeof module.exports === \"object\" ) {\n\t\t// For CommonJS and CommonJS-like environments where a proper `window`\n\t\t// is present, execute the factory and get jQuery.\n\t\t// For environments that do not have a `window` with a `document`\n\t\t// (such as Node.js), expose a factory as module.exports.\n\t\t// This accentuates the need for the creation of a real `window`.\n\t\t// e.g. var jQuery = require(\"jquery\")(window);\n\t\t// See ticket #14549 for more info.\n\t\tmodule.exports = global.document ?\n\t\t\tfactory( global, true ) :\n\t\t\tfunction( w ) {\n\t\t\t\tif ( !w.document ) {\n\t\t\t\t\tthrow new Error( \"jQuery requires a window with a document\" );\n\t\t\t\t}\n\t\t\t\treturn factory( w );\n\t\t\t};\n\t} else {\n\t\tfactory( global );\n\t}\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\";\nvar arr = [];\n\nvar document = window.document;","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/views/tag-helpers/th-components/samples/RazorPagesSample/wwwroot/lib/jquery/jquery.js#L11-L47","documentation":"jQuery's CommonJS branch returns a factory `function(w) { if (!w.document) throw ... }` when `global.document` is absent (e.g. Node.js). The factory is invoked with a `window`; if that window has no document, jQuery cannot manipulate the DOM and throws.","triggerScenarios":"Calling `require('jquery')(someWindow)` where `someWindow.document` is undefined; or `require('jquery')` in pure Node with no window at all triggers the factory path. Often seen in server-side rendering or test environments.","commonSituations":"Running jQuery-dependent code in Node during SSR or unit tests without jsdom; passing a minimal mock window that lacks document; importing a browser bundle into a Node test runner.","solutions":["Provide a real DOM via jsdom: `const { JSDOM } = require('jsdom'); const dom = new JSDOM(''); const $ = require('jquery')(dom.window);`.","Avoid requiring jQuery on the server; isolate DOM manipulation to the browser bundle.","Pass the full window object, not a partial mock: ensure `.document` exists before invoking the factory."],"exampleFix":"// before (Node, no window)\nconst $ = require('jquery');\n\n// after (jsdom provides window + document)\nconst { JSDOM } = require('jsdom');\nconst dom = new JSDOM('');\nconst $ = require('jquery')(dom.window);","handlingStrategy":"validation","validationCode":"function loadJQuery(windowLike) {\n  if (!windowLike || !windowLike.document) {\n    throw new Error('Provide a window with a document (use jsdom in Node)');\n  }\n  return require('jquery')(windowLike);\n}","typeGuard":"function hasDocument(w) {\n  return w != null && typeof w.document !== 'undefined';\n}","tryCatchPattern":"let $;\ntry {\n  const { JSDOM } = require('jsdom');\n  $ = require('jquery')(new JSDOM('').window);\n} catch (e) {\n  if (/window with a document/.test(e.message)) throw new Error('Install jsdom for server-side jQuery');\n  throw e;\n}","preventionTips":["In Node/SSR, always create a jsdom window before requiring jQuery.","Keep jQuery usage browser-only where possible.","Pass the full window object, never a partial mock."],"tags":["jquery","node","ssr","dom","environment"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}