ERR_INVALID_CURSOR_POS
Node.jsERRORCriticalValidationHIGH confidence

Cursor position is invalid for readline

Production Risk

Low — only affects CLI tools using readline cursor positioning.

What this means

Thrown when readline.cursorTo() or readline.moveCursor() is called without providing the stream argument as a TTY write stream, or when an invalid cursor position is specified. Cursor manipulation requires a TTY stream.

Why it happens
  1. 1Calling readline.cursorTo() without a y argument but still passing a callback as the third argument (which would be misinterpreted)
  2. 2Providing NaN or non-integer values for cursor coordinates
How to reproduce

Triggered when readline cursor functions receive invalid coordinate arguments.

trigger — this will error
trigger — this will error
const readline = require('readline');
readline.cursorTo(process.stdout, NaN, 5); // NaN x coordinate

expected output

TypeError [ERR_INVALID_CURSOR_POS]: Cannot set cursor row without setting its column

Fix

Provide valid integer coordinates to cursor functions

WHEN When using readline cursor positioning

Provide valid integer coordinates to cursor functions
const readline = require('readline');
if (process.stdout.isTTY) {
  readline.cursorTo(process.stdout, 0, 0); // move to top-left
  readline.clearLine(process.stdout, 0);
}

Why this works

Integer coordinates and a TTY check satisfy the readline cursor API requirements.

Code examples
Triggerjs
const readline = require('readline');
readline.cursorTo(process.stdout, NaN, 5); // NaN x coordinate  // this triggers ERR_INVALID_CURSOR_POS
Handle in try/catchjs
try {
  // operation that may throw ERR_INVALID_CURSOR_POS
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_INVALID_CURSOR_POS') {
    console.error('ERR_INVALID_CURSOR_POS:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_invalid_cursor_pos(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Call cursor positioning functions on non-TTY streams or with NaN coordinates

Cursor operations require a TTY and valid integer positions.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors