angular/components · error

Could not find <body> element in HTML file: ${htmlFileBuffer

Error message

Could not find <body> element in HTML file: ${htmlFileBuffer}

What it means

Thrown by the CDK schematics helper addBodyClass when the parsed HTML file contains no <body> element. The helper needs the body tag to attach a class attribute, so it throws when the target document lacks one.

Source

Thrown at src/cdk/schematics/utils/html-manipulation.ts:61

/** Parses the given HTML file and returns the head element if available. */
export function getHtmlHeadTagElement(htmlContent: string): Element | null {
  return getElementByTagName('head', htmlContent);
}

/** Adds a class to the body of the document. */
export function addBodyClass(host: Tree, htmlFilePath: string, className: string): void {
  const htmlFileBuffer = host.read(htmlFilePath);

  if (!htmlFileBuffer) {
    throw new SchematicsException(`Could not read file for path: ${htmlFilePath}`);
  }

  const htmlContent = htmlFileBuffer.toString();
  const body = getElementByTagName('body', htmlContent);

  if (!body) {
    throw Error(`Could not find <body> element in HTML file: ${htmlFileBuffer}`);
  }

  const classAttribute = body.attrs.find(attribute => attribute.name === 'class');

  if (classAttribute) {
    const hasClass = classAttribute.value
      .split(' ')
      .map(part => part.trim())
      .includes(className);

    if (!hasClass) {
      // We have source code location info enabled, and we pre-checked that the element
      // has attributes, specifically the `class` attribute.
      const classAttributeLocation = body.sourceCodeLocation!.attrs!['class'];
      const recordedChange = host
        .beginUpdate(htmlFilePath)
        .insertRight(classAttributeLocation.endOffset - 1, ` ${className}`);
      host.commitUpdate(recordedChange);

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Add a <body> element to the target HTML file and re-run the schematic
  2. Check the index path in angular.json points at the real full-document index.html
  3. Manually add the intended class to <body> if you cannot re-run the tooling
  4. Restore a well-formed index.html from version control

Example fix

<!-- before -->
<html><head></head></html>
<!-- after -->
<html>
  <head></head>
  <body class="app-loading"></body>
</html>
Defensive patterns

Strategy: validation

Validate before calling

const html = indexBuffer.toString();
if (!/<body[\s>]/i.test(html)) {
  throw new Error(`Cannot add body class: <body> element missing in ${indexPath}.`);
}

Type guard

function hasBodyElement(buffer: Buffer): boolean {
  return /<body[\s>]/i.test(buffer.toString());
}

Try / catch

try {
  addBodyClass(tree, indexPath, 'app-loading');
} catch (e) {
  if (e instanceof Error && e.message.includes('<body>')) {
    console.warn(`${indexPath} has no <body>; skipping class injection.`);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Running a schematic that adds a class to <body> against an HTML file without a body tag — a fragment, malformed markup, or a wrong file path configured as the index.

Common situations: Custom or generated index.html missing <body>; schematic configured to modify a partial template; file encoding/corruption breaking the HTML parser; running migration on non-app HTML assets.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/449b233f9f3c4c1a. Report an issue: GitHub.