Cursor position is invalid for readline
Production Risk
Low — only affects CLI tools using readline cursor positioning.
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.
- 1Calling readline.cursorTo() without a y argument but still passing a callback as the third argument (which would be misinterpreted)
- 2Providing NaN or non-integer values for cursor coordinates
Triggered when readline cursor functions receive invalid coordinate arguments.
const readline = require('readline');
readline.cursorTo(process.stdout, NaN, 5); // NaN x coordinateexpected 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
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.
const readline = require('readline');
readline.cursorTo(process.stdout, NaN, 5); // NaN x coordinate // this triggers ERR_INVALID_CURSOR_POStry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_invalid_cursor_pos(...args) {
// validate args here
return performOperation(...args)
}✕ Call cursor positioning functions on non-TTY streams or with NaN coordinates
Cursor operations require a TTY and valid integer positions.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev