Fixing error: “output is not a tty” in Node

If you write a Node script the prints output to the screen, and try to pipe the output to a file, you can get an error:

$ node extract.js > ideas.csv
output is not a tty

In my case, I was trying to write out a CSV file:

for (var k = 0; k < all.length; k++) {
  console.log(all[k]); 
}

The solution to this is to open the file directly and write to it:

var fs = require('fs');
var results = '';

for (var k = 0; k < all.length; k++) {
  results += all[k] + "\n";
}

fs.writeFile("ideas.csv", results);

The reason this doesn't work as expected appears to be that 'git bash' is passing the command through to the Windows cmd shell, and not handling the output stream correctly. Writing directly to a file avoids the issue entirely.

4 Replies to “Fixing error: “output is not a tty” in Node”

Leave a Reply to Saiyine Cancel reply

Your email address will not be published. Required fields are marked *