pbakaus/impeccable · error · Error

config.insertBefore or config.insertAfter (string) required

Error message

config.insertBefore or config.insertAfter (string) required

What it means

validateConfig() requires an insertion anchor: cfg.insertBefore or cfg.insertAfter (or both) must be a string. The check throws only when NEITHER is a string — both missing, or both present with non-string values (numbers, null). The anchor tells the injector where the script/comment block goes relative to existing markup.

Solutions

  1. Add a string anchor, e.g. "insertAfter": "</title>" or "insertBefore": "</body>" — the literal markup the injector searches for
  2. Ensure the value is a quoted string, not a number or null
  3. Pick an anchor that actually exists in the target files, otherwise the inject run fails later with a not-found error

Example fix

// before
{ "files": ["index.html"], "commentSyntax": "html" }

// after
{ "files": ["index.html"], "insertAfter": "</title>", "commentSyntax": "html" }
Defensive patterns

Strategy: validation

Validate before calling

const hasAnchor = typeof cfg.insertBefore === 'string' || typeof cfg.insertAfter === 'string';
if (!hasAnchor) throw new TypeError('config needs insertBefore or insertAfter as a string');
// Bonus: confirm the anchor actually occurs in a target file before injecting.
import fs from 'node:fs';
const sample = cfg.files.find((f) => fs.existsSync(f));
if (sample && !fs.readFileSync(sample, 'utf-8').includes(cfg.insertAfter ?? cfg.insertBefore)) {
  console.warn('anchor string not found in sample file — inject will fail later');
}

Type guard

const hasInsertAnchor = (cfg) =>
  typeof cfg?.insertBefore === 'string' || typeof cfg?.insertAfter === 'string';

Prevention

When it happens

Trigger: Config has neither insertBefore nor insertAfter; or insertBefore: 123 / null while insertAfter is also absent or non-string. Providing exactly one string (the normal case) or both as strings passes.

Common situations: Trimmed-down configs copied from another project that relied on a default (there is no default anchor); renaming the key (e.g. 'insertBeforeTag') during hand edits; YAML-to-JSON conversions dropping empty values.

Related errors


AI-assisted analysis of pbakaus/impeccable@f88b2837a7 (2026-08-18). Data as JSON: /api/errors/7b782715d73ba754. Report an issue: GitHub.

Appendix: source

Thrown at skill/scripts/live-inject.mjs:466

function validateConfig(cfg) {
  if (!cfg || typeof cfg !== 'object') throw new Error('config.json must be an object');
  if (!Array.isArray(cfg.files) || cfg.files.length === 0) {
    throw new Error('config.files (non-empty string array) required');
  }
  if (!cfg.files.every((f) => typeof f === 'string' && f.length > 0)) {
    throw new Error('config.files must contain only non-empty strings');
  }
  if (cfg.exclude !== undefined) {
    if (!Array.isArray(cfg.exclude)) {
      throw new Error('config.exclude, if present, must be a string array');
    }
    if (!cfg.exclude.every((f) => typeof f === 'string' && f.length > 0)) {
      throw new Error('config.exclude must contain only non-empty strings');
    }
  }
  if (typeof cfg.insertBefore !== 'string' && typeof cfg.insertAfter !== 'string') {
    throw new Error('config.insertBefore or config.insertAfter (string) required');
  }
  if (cfg.commentSyntax !== 'html' && cfg.commentSyntax !== 'jsx') {
    throw new Error("config.commentSyntax must be 'html' or 'jsx'");
  }
  if (cfg.cspChecked !== undefined && typeof cfg.cspChecked !== 'boolean') {
    throw new Error("config.cspChecked, if present, must be a boolean");
  }
}

// ---------------------------------------------------------------------------
// Auto-execute
// ---------------------------------------------------------------------------

const _running = process.argv[1];
if (_running?.endsWith('live-inject.mjs') || _running?.endsWith('live-inject.mjs/')) {
  enterLiveRoot();
  injectCli();
}

View on GitHub (pinned to f88b2837a7)