napi-rs/napi-rs · error · napi::Error (Status::StringExpected)

Invalid release name

Error message

Invalid release name

What it means

When converting the raw `napi_node_version` struct into the safe `NodeVersion`, the `release` C string pointer (e.g. "v20.11.0") is not valid UTF-8, so `CStr::from_ptr(...).to_str()` fails and napi-rs returns `Status::StringExpected` with this message. This indicates corrupted or non-UTF-8 data in the underlying Node version info.

Solutions

  1. Run against a standard Node.js release; verify `process.release.name` is a normal UTF-8 string.
  2. If using a custom Node/Electron build, check the build script that sets the release version string and ensure it is UTF-8.
  3. If you control the call, handle the `Result` and fall back to a default version string instead of propagating the error.
  4. Report/inspect the N-API shim if running under a non-Node runtime that implements `napi_get_node_version` incorrectly.

Example fix

// before
let version = env.node_version()?;

// after
let version = env.node_version().unwrap_or(NodeVersion {
  major: 0, minor: 0, patch: 0,
  release: "unknown".to_owned(),
});
Defensive patterns

Strategy: try-catch

Validate before calling

// Sanity-check the runtime before reading version info
if (typeof process === 'undefined' || typeof process.release?.name !== 'string') {
  throw new Error('Running under a runtime that may not provide a valid napi_get_node_version');
}

Type guard

function isValidNodeVersion(v) {
  return v != null && typeof v.release === 'string' && v.release.length > 0;
}

Try / catch

let version;
try {
  version = env.node_version()?;
} catch (e) {
  if (e.status === generic_failure && e.message === 'Invalid release name') {
    version = fallbackVersion(); // or log and degrade
  } else {
    return Err(e);
  }
}

Prevention

When it happens

Trigger: Calling `napi_get_node_version` (exposed as `process.version`-style APIs in napi-rs) on a Node build whose `release` string contains invalid UTF-8 bytes, or a hostile/custom N-API shim returning a malformed release pointer.

Common situations: Custom/embedded Node builds or Electron forks with patched version strings; fuzzing or mocking the N-API layer with garbage pointers; extremely unusual locale-encoded build metadata.

Related errors


AI-assisted analysis of napi-rs/napi-rs@39bd1205e4 (2026-09-13). Data as JSON: /api/errors/4c2ba9a2cecf3ff6. Report an issue: GitHub.

Appendix: source

Thrown at crates/napi/src/version.rs:23

pub struct NodeVersion {
  pub major: u32,
  pub minor: u32,
  pub patch: u32,
  pub release: &'static str,
}

impl TryFrom<sys::napi_node_version> for NodeVersion {
  type Error = Error;

  fn try_from(value: sys::napi_node_version) -> Result<NodeVersion, Error> {
    Ok(NodeVersion {
      major: value.major,
      minor: value.minor,
      patch: value.patch,
      release: unsafe {
        CStr::from_ptr(value.release)
          .to_str()
          .map_err(|_| Error::new(Status::StringExpected, "Invalid release name".to_owned()))?
      },
    })
  }
}

View on GitHub (pinned to 39bd1205e4)