angular/angular-cli · error · Error

Invalid semver version for ${this.name}: "${this.#version}"

Error message

Invalid semver version for ${this.name}: "${this.#version}"

What it means

getVersion() runs the package manager's version command (e.g. `npm --version`), trims stdout, and validates the result with semver's `valid()`. If the output is not a valid semver string, this Error is thrown naming the package manager and the offending raw output. The library enforces semver because the version is used for feature comparisons and compatibility checks.

Source

Thrown at packages/angular/cli/src/package-managers/package-manager.ts:429

      return pkgJson.name;
    } catch {
      return undefined;
    }
  }

  /**
   * Gets the version of the package manager binary.
   */
  async getVersion(): Promise<string> {
    if (this.#version) {
      return this.#version;
    }

    const { stdout } = await this.#run(this.descriptor.versionCommand);
    this.#version = stdout.trim();

    if (!valid(this.#version)) {
      throw new Error(`Invalid semver version for ${this.name}: "${this.#version}"`);
    }

    return this.#version;
  }

  /**
   * Gets the installed details of a package from the project's dependencies.
   * @param packageName The name of the package to check.
   * @returns A promise that resolves to the installed package details, or `null` if the package is not installed.
   */
  async getInstalledPackage(packageName: string): Promise<InstalledPackage | null> {
    const cache = await this.#populateDependencyCache();

    return cache.get(packageName) ?? null;
  }

  /**
   * Gets a map of all top-level dependencies installed in the project.

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run the version command manually (`npm --version` / `yarn --version` / `pnpm --version`) and inspect the raw output for extra text.
  2. Remove shell profile lines or wrapper scripts that echo output before the version string.
  3. Reinstall the package manager from the official channel (`npm i -g npm@latest`, corepack) to replace nonstandard builds.
  4. Ensure the binary on PATH is the real package manager (`which npm`) and not a shim printing extra text.
  5. If a prerelease with unusual formatting is required, use one whose version output passes semver.valid().

Example fix

// before: shell profile prints banner on every command
// echo "Welcome to my shell"   <- in ~/.bashrc, pollutes captured output

// after: guard profile output for non-interactive shells
echo 'command -v npm >/dev/null 2>&1 && [ -z "$PS1" ] || echo "Welcome to my shell"' >> ~/.bashrc
Defensive patterns

Strategy: validation

Validate before calling

import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { valid } from 'semver';
const run = promisify(exec);
const { stdout } = await run('npm --version');
if (!valid(stdout.trim())) {
  throw new Error(`Package manager reports invalid version: "${stdout.trim()}"`);
}

Try / catch

try {
  const version = await pm.getVersion();
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Invalid semver version for')) {
    console.error('Package manager output is not a valid semver:', e.message);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling version() or acquireTempPackage() (which call getVersion()) when the version command prints something that is not a bare semver version — e.g. extra labels, warning text, or a non-version string.

Common situations: Package manager installed from a nonstandard source (nightly, fork, OS distro patch) printing versions like '1.2.3-beta.0+local' variants or '8.19.2 (custom build)'; shell profile echoing banners before command output; wrapper scripts printing deprecation notices; a corrupted or shimmed binary on PATH.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/de2824a4109fc07b. Report an issue: GitHub.