{"record":{"id":"83e5abaa8357e6d7","repo":"mozilla/pdf.js","slug":"invalid-container-and-or-viewer-option","errorCode":null,"errorMessage":"Invalid `container` and/or `viewer` option.","messagePattern":"Invalid `container` and/or `viewer` option\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"web/pdf_viewer.js","lineNumber":325,"sourceCode":"  /**\n   * @param {PDFViewerOptions} options\n   */\n  constructor(options) {\n    const viewerVersion =\n      typeof PDFJSDev !== \"undefined\" ? PDFJSDev.eval(\"BUNDLE_VERSION\") : null;\n    if (version !== viewerVersion) {\n      throw new Error(\n        `The API version \"${version}\" does not match the Viewer version \"${viewerVersion}\".`\n      );\n    }\n\n    this.container = options.container;\n    this.viewer = options.viewer || options.container.firstElementChild;\n    this.#viewerAlert = options.viewerAlert || null;\n\n    if (typeof PDFJSDev === \"undefined\" || PDFJSDev.test(\"GENERIC\")) {\n      if (this.container?.tagName !== \"DIV\" || this.viewer?.tagName !== \"DIV\") {\n        throw new Error(\"Invalid `container` and/or `viewer` option.\");\n      }\n\n      if (\n        this.container.offsetParent &&\n        getComputedStyle(this.container).position !== \"absolute\"\n      ) {\n        throw new Error(\"The `container` must be absolutely positioned.\");\n      }\n    }\n    this.#resizeObserver.observe(this.container);\n\n    this.eventBus = options.eventBus;\n    this.linkService = options.linkService || new SimpleLinkService();\n    this.downloadManager = options.downloadManager || null;\n    this.findController = options.findController || null;\n    this.#altTextManager = options.altTextManager || null;\n    this.#commentManager = options.commentManager || null;\n    this.#signatureManager = options.signatureManager || null;","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/web/pdf_viewer.js#L307-L343","documentation":"Thrown by the PDFViewer constructor (GENERIC build only) when the container or viewer element is not a <div>. The viewer relies on specific DIV structure to manage page views and scrolling.","triggerScenarios":"Passing options.container or options.viewer that is not a DIV (e.g. a <section>, or null/undefined where tagName check fails). Only enforced when PDFJSDev is undefined or GENERIC build.","commonSituations":"Custom HTML using a non-div container; container element not found (null) because selector missed; passing the wrong DOM node in options.","solutions":["Ensure options.container is a <div> element and its firstElementChild (or options.viewer) is also a <div>.","Verify the DOM is ready and the selector returns the expected element before constructing PDFViewer.","Adopt the standard viewer markup structure from web/viewer.html."],"exampleFix":"// before\nnew PDFViewer({ container: document.querySelector('section#viewer') });\n\n// after\nnew PDFViewer({\n  container: document.querySelector('div#viewerContainer'),\n  viewer: document.querySelector('div#viewer'),\n});","handlingStrategy":"validation","validationCode":"const container = document.querySelector('div#viewerContainer');\nconst viewer = document.querySelector('div#viewer');\nif (container?.tagName === 'DIV' && viewer?.tagName === 'DIV') {\n  new PDFViewer({ container, viewer });\n}","typeGuard":"function isDiv(el) {\n  return el instanceof HTMLElement && el.tagName === 'DIV';\n}","tryCatchPattern":null,"preventionTips":["Use the standard viewer markup (DIV for container and viewer).","Resolve elements after DOMContentLoaded.","Verify selectors return the expected node type."],"tags":["configuration","dom","constructor","generic-build"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}