denoland/deno · error · ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

Error message

The argument 'x' is invalid. Received ${x}

What it means

readline.cursorTo validates its x coordinate: if x is NaN it throws ERR_INVALID_ARG_VALUE immediately, before the stream checks. x is a 0-based column for the ANSI cursor sequence, so NaN (not undefined — non-numbers take an early no-op path) can only come from arithmetic or explicit NaN.

Source

Thrown at ext/node/polyfills/internal/readline/callbacks.mjs:64

  kClearToLineBeginning,
  kClearToLineEnd,
} = CSI;

/**
 * moves the cursor to the x and y coordinate on the given stream
 */

function cursorTo(stream, x, y, callback) {
  if (callback !== undefined) {
    validateFunction(callback, "callback");
  }

  if (typeof y === "function") {
    callback = y;
    y = undefined;
  }

  if (NumberIsNaN(x)) throw new ERR_INVALID_ARG_VALUE("x", x);
  if (NumberIsNaN(y)) throw new ERR_INVALID_ARG_VALUE("y", y);

  if (stream == null || (typeof x !== "number" && typeof y !== "number")) {
    if (typeof callback === "function") process.nextTick(callback, null);
    return true;
  }

  if (typeof x !== "number") throw new ERR_INVALID_CURSOR_POS();

  const data = typeof y !== "number" ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H`;
  return stream.write(data, callback);
}

/**
 * moves the cursor relative to its current location
 */

function moveCursor(stream, dx, dy, callback) {

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Check Number.isFinite(x) before calling cursorTo and skip rendering when not
  2. Default the column: const col = Number.isFinite(x) ? x : 0
  3. Fix the upstream width source (process.stdout.columns ?? 80) that produced NaN

Example fix

// before
readline.cursorTo(stream, pos.col - 1); // pos.col undefined -> NaN

// after
readline.cursorTo(stream, (pos.col ?? 1) - 1);
Defensive patterns

Strategy: validation

Validate before calling

function cursorCol(stream: NodeJS.WriteStream, x: number) {
  if (Number.isFinite(x)) readline.cursorTo(stream, x);
}

Type guard

const isFiniteCoord = (v: unknown): v is number =>
  typeof v === 'number' && Number.isFinite(v);

Try / catch

try {
  readline.cursorTo(stream, x);
} catch (err) {
  if (err?.code === 'ERR_INVALID_ARG_VALUE' && /x/.test(err.message)) {
    readline.cursorTo(stream, 0);
  } else throw err;
}

Prevention

When it happens

Trigger: readline.cursorTo(process.stdout, NaN); x computed as width - something where width is undefined (undefined - 1 = NaN); passing parseFloat(userInput) when the input is not numeric.

Common situations: Terminal UI/progress renderers computing columns from terminal width that is unavailable (output redirected, not a TTY); parseFloat of empty string; division by zero in layout math.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/481ccefa7efcf04b. Report an issue: GitHub.