humanify icon indicating copy to clipboard operation
humanify copied to clipboard

Error: process.stdout.cursorTo is not a function

Open 0xdevalias opened this issue 5 months ago • 0 comments

Creating a new issue to track this as #57 was incorrectly closed, and I don't have the access to re-open it as valid:

  • duplicates #57

process.stdout.cursorTo is only defined when the shell you're running in isn't capturing the output. You need to check process.stdout.isTTY before trying to access it.

Source: https://nodejs.org/api/process.html#a-note-on-process-io https://nodejs.org/api/tty.html#writestreamcursortox-y-callback

Originally posted by @ItsHarper in https://github.com/jehna/humanify/issues/57#issuecomment-3111924889


process.stdout.cursorTo is only defined when the shell you're running in isn't capturing the output. You need to check process.stdout.isTTY before trying to access it.

  • https://nodejs.org/api/process.html#a-note-on-process-io
    • To check if a stream is connected to a TTY context, check the isTTY property.

  • https://nodejs.org/api/tty.html
    • When Node.js detects that it is being run with a text terminal ("TTY") attached, process.stdin will, by default, be initialized as an instance of tty.ReadStream and both process.stdout and process.stderr will, by default, be instances of tty.WriteStream. The preferred method of determining whether Node.js is being run within a TTY context is to check that the value of the process.stdout.isTTY property is true

Is it possible by any chance that you're trying to run humanify using an old node.js? It seems that stdout.cursorTo is introduced in node.js version 17, so this implies that your node.js version would have been under that.

Based on the above, we can see this behaviour in node v22, so it wasn't just an issue with running an older version of node:

⇒ node --version
v22.16.0
⇒ node -p -e "Boolean(process.stdout.isTTY)"
true

⇒ node -p -e "Boolean(process.stdout.isTTY)" | cat
false
⇒ node -p -e "process.stdout.cursorTo(0)"
true

⇒ node -p -e "process.stdout.cursorTo(0)" | cat
[eval]:1
process.stdout.cursorTo(0)
               ^

TypeError: process.stdout.cursorTo is not a function
    at [eval]:1:16
    at runScriptInThisContext (node:internal/vm:209:10)
    at node:internal/process/execution:449:12
    at [eval]-wrapper:6:24
    at runScriptInContext (node:internal/process/execution:447:60)
    at evalFunction (node:internal/process/execution:87:30)
    at evalScript (node:internal/process/execution:99:3)
    at node:internal/main/eval_string:74:3

Node.js v22.16.0

I'll fix the underlying issue in coming versions.

@jehna So this is still a valid issue that should be fixed, and this issue should be re-opened to track that.

Usage:

https://github.com/jehna/humanify/blob/7beba2d32433e58bb77d0e1b0eda01c470fec3e2/src/progress.ts#L25-L31

Example of a potential fix:

export function showPercentage(percentage: number) {
  const percentageStr = Math.round(percentage * 100);

  const canUseCursorOps = process.stdout.isTTY
    && typeof process.stdout.clearLine === "function"
    && typeof process.stdout.cursorTo === "function";

  if (!verbose.enabled && canUseCursorOps) {
    process.stdout.clearLine(0);
    process.stdout.cursorTo(0);
    process.stdout.write(`Processing: ${percentageStr}%`);
  } else {
    verbose.log(`Processing: ${percentageStr}%`);
  }

  if (percentage === 1 && canUseCursorOps) {
    process.stdout.write("\n");
  }
}

Ideally a similar more explicit fix would be added to clearLine in showProgress as well, instead of just relying on the optional chaining syntax.


You can see that this would also be an issue with process.stdout.clearLine:

⇒ node -p -e "process.stdout.clearLine(0)" | cat
[eval]:1
process.stdout.clearLine(0)
               ^

TypeError: process.stdout.clearLine is not a function
    at [eval]:1:16
    at runScriptInThisContext (node:internal/vm:209:10)
    at node:internal/process/execution:449:12
    at [eval]-wrapper:6:24
    at runScriptInContext (node:internal/process/execution:447:60)
    at evalFunction (node:internal/process/execution:87:30)
    at evalScript (node:internal/process/execution:99:3)
    at node:internal/main/eval_string:74:3

Node.js v22.16.0

Except that in the code, you're calling process.stdout.clearLine with the optional chaining syntax like this:

https://github.com/jehna/humanify/blob/7beba2d32433e58bb77d0e1b0eda01c470fec3e2/src/progress.ts#L10

https://github.com/jehna/humanify/blob/7beba2d32433e58bb77d0e1b0eda01c470fec3e2/src/progress.ts#L28

Whereas for process.stdout.cursorTo you're not using the optional chaining syntax:

https://github.com/jehna/humanify/blob/7beba2d32433e58bb77d0e1b0eda01c470fec3e2/src/progress.ts#L29

Looking at the git blame / history, the progress feature seemed to be first added in 70e31966e405c60790073d032042e2b39fe448ce on Aug 9, 2024, and neither used the optional chaining syntax:

  • https://github.com/jehna/humanify/commit/70e31966e405c60790073d032042e2b39fe448ce#diff-ae7dc0c4f05cb30c61f24ee7a16b994a4901803bdbcaf6c7dbedd40d6fd11386R24-R32

But then the optional chaining was added to clearLine in 05b6a8e261ebc6c9c4221e0ae1c34fe38c4531aa on Aug 9, 2024 as it broke the CI:

  • https://github.com/jehna/humanify/commit/05b6a8e261ebc6c9c4221e0ae1c34fe38c4531aa

Originally posted by @0xdevalias in https://github.com/jehna/humanify/issues/57#issuecomment-3123626842

0xdevalias avatar Jul 27 '25 00:07 0xdevalias