AUR0769
Error Message
AUR0769: Failed to initialize the platform object. The host element's ownerDocument does not have a defaultView, did you create the host from a DOMParser and forget to call adoptNode()?
Description
This error occurs during the setup of the application's root component (AppRoot
) when Aurelia attempts to access the global window
object (via hostElement.ownerDocument.defaultView
). The error indicates that the defaultView
property is missing or null on the document that owns the host element provided to aurelia.app({ host: ... })
. This typically means the host element exists within a document context that is disconnected from the main browser window environment.
Cause
Host Created by
DOMParser
without Adoption: Creating an HTML structure usingnew DOMParser().parseFromString(...)
and then selecting a host element from the resulting document without importing or adopting that element into the maindocument
usingdocument.importNode()
ordocument.adoptNode()
. Elements parsed this way belong to a separate, disconnected document object initially.Host Created by
document.implementation.createHTMLDocument
: Creating a full document usingdocument.implementation.createHTMLDocument()
and using an element from this document as the host. Similar toDOMParser
, this document is not the main browser document and lacks adefaultView
unless specifically associated with a browsing context.Running in Non-Browser Environment without Polyfills: Attempting to run Aurelia with
@aurelia/runtime-html
in an environment that doesn't provide a standard DOM with awindow
/defaultView
(like certain server-side rendering scenarios or minimal JS environments) without appropriate polyfills or a compatible platform implementation.
Solution
Use
document.importNode()
: If creating the host element or its parent structure usingDOMParser
orcreateHTMLDocument
, ensure you import the relevant node(s) into the maindocument
before passing the host element to Aurelia. Useconst importedNode = document.importNode(nodeFromParser, true);
and appendimportedNode
to the live DOM.Select Host from Live DOM: Ensure the
host
element provided toaurelia.app()
is selected from the active browser document (e.g., usingdocument.getElementById
,document.querySelector
). Do not use elements directly from documents created in memory without importing them.Provide Correct Platform: In non-standard environments, ensure you provide a suitable
IPlatform
implementation that correctly polyfills or represents the necessary DOM and window features. For standard browser usage, this is typically not required.
Example
// main.ts (or equivalent setup code)
import Aurelia from 'aurelia';
import { MyApp } from './my-app';
// Incorrect: Using element directly from DOMParser result
const parser = new DOMParser();
const htmlString = `<html><body><div id="app"></div></body></html>`;
const parsedDoc = parser.parseFromString(htmlString, 'text/html');
const hostFromParser = parsedDoc.getElementById('app'); // Belongs to parsedDoc, not main document
try {
// This will likely throw AUR0769 because hostFromParser.ownerDocument has no defaultView
const aureliaInstance = new Aurelia()
.app({ host: hostFromParser, component: MyApp })
.start();
} catch (e) {
console.error("Failed to start Aurelia:", e);
}
// Correct: Import the node into the main document first
const mainDocumentHost = document.createElement('div'); // Create a container in the live document
mainDocumentHost.id = 'app-container';
document.body.appendChild(mainDocumentHost);
const nodeToImport = parsedDoc.body.firstChild; // Get the <div id="app"> from the parsed doc
if (nodeToImport) {
const importedHost = document.importNode(nodeToImport, true); // Import into main document
mainDocumentHost.appendChild(importedHost); // Append to live DOM
// Now use the importedHost, which belongs to the main document
const aureliaCorrect = new Aurelia()
.app({ host: importedHost as HTMLElement, component: MyApp })
.start()
.then(() => console.log("Aurelia started correctly with imported node."))
.catch(err => console.error("Corrected startup failed:", err));
}
// Simpler Correct: Select host directly from the main document
const directHost = document.getElementById('some-existing-div-in-index-html');
if (directHost) {
const aureliaDirect = new Aurelia()
.app({ host: directHost, component: MyApp })
.start()
.then(() => console.log("Aurelia started correctly with direct host."))
.catch(err => console.error("Direct host startup failed:", err));
}
Debugging Tips
Inspect the
host
element being passed toaurelia.app()
.Check
hostElement.ownerDocument
in the debugger.Verify that
hostElement.ownerDocument.defaultView
is the mainwindow
object and notnull
orundefined
.If the host is created dynamically, trace its origin. Ensure it was either created directly within the main
document
or properly imported/adopted usingdocument.importNode
ordocument.adoptNode
.
Last updated
Was this helpful?