coleam00/Archon · error · Error

Failed to read version: package.json is malformed

Error message

Failed to read version: package.json is malformed

What it means

getDevVersion() reads the monorepo root package.json at runtime when the CLI runs in development (non-binary) mode. After a successful read, JSON.parse of the file contents is wrapped in try/catch; any parse failure becomes this error. It indicates the root package.json exists but is not valid JSON, so the version/name cannot be reported.

Source

Thrown at packages/cli/src/commands/version.ts:53

  let content: string;
  try {
    content = await readFile(pkgPath, 'utf-8');
  } catch (error) {
    const err = error as NodeJS.ErrnoException;
    if (err.code === 'ENOENT') {
      throw new Error('Failed to read version: package.json not found (bad installation?)');
    } else if (err.code === 'EACCES') {
      throw new Error('Failed to read version: permission denied reading package.json');
    }
    throw new Error(`Failed to read version: ${err.message}`);
  }

  let pkg: PackageJson;
  try {
    pkg = JSON.parse(content) as PackageJson;
  } catch (_error) {
    throw new Error('Failed to read version: package.json is malformed');
  }

  return { name: pkg.name, version: pkg.version };
}

/**
 * Get the git commit hash at runtime (dev mode).
 * Returns 'unknown' if git is unavailable or the command fails.
 */
async function getDevGitCommit(): Promise<string> {
  try {
    const { stdout } = await execFileAsync('git', ['rev-parse', '--short', 'HEAD'], {
      timeout: 5000,
    });
    return stdout.trim();
  } catch (err) {
    // Non-blocking: git may not be installed or cwd may not be a git repo
    log.debug({ err }, 'version.git_commit_lookup_failed');

View on GitHub (pinned to 0773b97458)

Solutions

  1. Validate the root package.json with `bun pm` or `node -e "JSON.parse(require('fs').readFileSync('package.json','utf8'))"` to see the exact parse error.
  2. Fix the JSON by removing conflict markers, trailing commas, comments, or a BOM (UTF-8 without BOM).
  3. Restore the file from git: `git checkout -- package.json` if it was committed and only locally corrupted.
  4. Reinstall the checkout (fresh clone or `bun install`) if the corruption came from a tool.

Example fix

// package.json (before — invalid)
{ "name": "archon", "version": "0.3.1", }
// after
{ "name": "archon", "version": "0.3.1" }
Defensive patterns

Strategy: validation

Validate before calling

import { readFileSync } from 'fs';
try {
  const content = readFileSync('package.json', 'utf-8');
  JSON.parse(content); // throws with position info before the CLI ever runs
} catch (e) {
  console.error('package.json is not valid JSON:', (e as Error).message);
}

Type guard

function isValidPackageJson(s: string): boolean {
  try {
    const p: unknown = JSON.parse(s);
    return typeof p === 'object' && p !== null && 'name' in p && 'version' in p;
  } catch {
    return false;
  }
}

Try / catch

try {
  await versionCommand();
} catch (e) {
  if ((e as Error).message.startsWith('Failed to read version')) {
    // fix or restore package.json, or pin a binary release which embeds the version
  }
}

Prevention

When it happens

Trigger: Running `archon version` (via devInfo -> getDevVersion) in a source checkout whose root package.json was hand-edited, truncated by an interrupted merge/checkout, saved with a trailing comma or BOM, or corrupted so JSON.parse throws.

Common situations: Manual edit of package.json that left invalid JSON; a git merge conflict left conflict markers in the file; an interrupted npm/bun install wrote a partial file; a text editor or tool rewrote the file with a byte-order mark or comments.

Understand the failure class

Related errors


AI-assisted analysis of coleam00/Archon@0773b97458 (2026-09-01). Data as JSON: /api/errors/df9f77e4e82054c9. Report an issue: GitHub.