denoland/deno · error · AnyError

Invalid version req

Error message

Invalid version req '{req_str}': {e}

What it means

Thrown by resolve_jsr_version() when the requested version string is neither an exact published version nor parseable by VersionReq::parse_from_npm; the message embeds the offending requirement plus the parser's reason. An exact version that exists in metadata returns early, so this only fires for malformed range strings.

Solutions

  1. Correct the range to valid npm semver syntax: `^1.2.3`, `~1.2`, `>=1.0.0 <2`, or an exact `1.2.3`
  2. Replace dist-tag names (next, beta, canary) with the concrete version they currently point to
  3. Check the version string for stray spaces, double operators, or quotes in deno.json and the add command

Example fix

// before
{
  "imports": { "@scope/pkg": "jsr:@scope/pkg@next" }
}

// after
{
  "imports": { "@scope/pkg": "jsr:@scope/pkg@^1.4.0" }
}
Defensive patterns

Strategy: validation

Validate before calling

import semver from 'npm:semver';
const range = '^1.2.x';
if (!semver.validRange(range)) {
  throw new Error(`Invalid semver range: ${range}`);
}

Type guard

const isValidRange = (r: string): boolean => {
  try { return semver.validRange(r) !== null; } catch { return false; }
};

Prevention

When it happens

Trigger: Adding a dependency with a malformed npm semver range — `deno add pkg@next`, `pkg@1.x.3`, `pkg@^1.0.0.0`, an empty string, or ranges with spaces/typos like `^^1.0.0` — so VersionReq::parse_from_npm returns Err at cli/tools/installer/npm_compat.rs:1468.

Common situations: Copying a git branch or dist-tag name into the version slot; generated configs writing build numbers like 1.2.3.4; trailing whitespace or smart quotes in deno.json version fields; hand-editing import specifiers.

Related errors


AI-assisted analysis of denoland/deno@336da420f4 (2026-08-20). Data as JSON: /api/errors/1b948c471d3d4c05. Report an issue: GitHub.

Appendix: source

Thrown at cli/tools/installer/npm_compat.rs:1599

  match req_version {
    None => metadata
      .get("dist-tags")
      .and_then(|dt| dt.get("latest"))
      .and_then(|v| v.as_str())
      .map(|s| s.to_string())
      .ok_or_else(|| anyhow!("No latest version for {registry_name}")),
    Some(req_str) => {
      if let Ok(exact) = Version::parse_standard(req_str)
        && metadata
          .get("versions")
          .and_then(|vs| vs.get(exact.to_string()))
          .is_some()
      {
        return Ok(exact.to_string());
      }

      let version_req = VersionReq::parse_from_npm(req_str)
        .map_err(|e| anyhow!("Invalid version req '{req_str}': {e}"))?;

      let versions = metadata
        .get("versions")
        .and_then(|vs| vs.as_object())
        .ok_or_else(|| anyhow!("No versions for {registry_name}"))?;

      let mut best: Option<Version> = None;
      for key in versions.keys() {
        if let Ok(v) = Version::parse_standard(key)
          && version_req.matches(&v)
          && best.as_ref().is_none_or(|b| v > *b)
        {
          best = Some(v);
        }
      }

      best.map(|v| v.to_string()).ok_or_else(|| {
        anyhow!("No version matching '{req_str}' for {registry_name}")

View on GitHub (pinned to 336da420f4)