# 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

1. **Host Created by `DOMParser` without Adoption:** Creating an HTML structure using `new DOMParser().parseFromString(...)` and then selecting a host element from the resulting document *without* importing or adopting that element into the main `document` using `document.importNode()` or `document.adoptNode()`. Elements parsed this way belong to a separate, disconnected document object initially.
2. **Host Created by `document.implementation.createHTMLDocument`:** Creating a full document using `document.implementation.createHTMLDocument()` and using an element from this document as the host. Similar to `DOMParser`, this document is not the main browser document and lacks a `defaultView` unless specifically associated with a browsing context.
3. **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 a `window`/`defaultView` (like certain server-side rendering scenarios or minimal JS environments) without appropriate polyfills or a compatible platform implementation.

## Solution

1. **Use `document.importNode()`:** If creating the host element or its parent structure using `DOMParser` or `createHTMLDocument`, ensure you import the relevant node(s) into the main `document` before passing the host element to Aurelia. Use `const importedNode = document.importNode(nodeFromParser, true);` and append `importedNode` to the live DOM.
2. **Select Host from Live DOM:** Ensure the `host` element provided to `aurelia.app()` is selected from the active browser document (e.g., using `document.getElementById`, `document.querySelector`). Do not use elements directly from documents created in memory without importing them.
3. **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

```typescript
// 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 to `aurelia.app()`.
* Check `hostElement.ownerDocument` in the debugger.
* Verify that `hostElement.ownerDocument.defaultView` is the main `window` object and not `null` or `undefined`.
* If the host is created dynamically, trace its origin. Ensure it was either created directly within the main `document` or properly imported/adopted using `document.importNode` or `document.adoptNode`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aurelia.io/developer-guides/error-messages/runtime-html/aur0769.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
