{"record":{"id":"ea5726a2a311bd36","repo":"dotnet/AspNetCore.Docs","slug":"jquery-requires-a-window-with-a-document-ea5726","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/3.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/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/jquery-2.2.0.js#L11-L47","documentation":"Identical guard to error 2 in the 3.x sample's jquery-2.2.0.js: the UMD CommonJS factory requires a window with a `.document` property. Calling it with a non-window object throws.","triggerScenarios":"`require('jquery')` in Node without jsdom and invoking the factory with a non-window; SSR without a DOM shim.","commonSituations":"Server-side rendering; Node unit tests; Electron main process.","solutions":["Pass a jsdom window: require('jquery')(new (require('jsdom').JSDOM)('').window).","Use cheerio for non-DOM jQuery-style parsing in Node.","Ensure the passed object exposes both `document` and `window`."],"exampleFix":"// before\nconst $ = require('jquery')({}); // throws\n// after\nconst { JSDOM } = require('jsdom');\nconst $ = require('jquery')(new JSDOM('').window);","handlingStrategy":"type-guard","validationCode":"function 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) throw new Error('No DOM for jQuery');\nvar $ = require('jquery')(w);","typeGuard":"function isWindowLike(w) { return w != null && typeof w === 'object' && !!w.document && typeof w.document.createElement === 'function'; }","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":["Pair jQuery with jsdom in Node; pass dom.window.","Use cheerio for pure HTML parsing.","Never pass an empty object or 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"}