0

I'm starting a CLI process with node.js on Windows that outputs data to stdout which I capture on "data"

const { spawn } = require('child_process');
const process = spawn(path);

process.stdout.on("data", (data) => {
  const message = data.toString("utf16le").replace(/\0/g, "");
  console.log(message);
});

Then problem is that I only get data when a new line has been emitted. Sometimes multiple lines at the same time (which is fine), but always with a new line at the end. Sometimes the application emits the last line without a new line, so I don't get that line until the process continues with the next output.

Is it possible to read stream data without new lines?

mottosson
  • 3,283
  • 4
  • 35
  • 73
  • Maybe [this](https://stackoverflow.com/questions/27889887/is-possible-to-read-independently-every-stdout-flushed-string-form-a-child-proce) could give you a lead. My guess is that somehow the data get flushed by the child only when being full or when a `\n` happend – Orelsanpls Oct 04 '19 at 11:57
  • [here](https://stackoverflow.com/questions/18849112/stream-child-process-output-in-flowing-mode) there is a good answer about using `-u` using python. Maybe there is something for you. What is the type of the program you are trying to spawn? – Orelsanpls Oct 04 '19 at 12:02
  • AutoCAD CLI (called accoreconsole) – mottosson Oct 04 '19 at 12:03
  • @mottosson You edited your question, rephrased it. Multiple edits cost people time, please write up your original final post first, then ask the question. Because you were asking something totally different. – ABC Oct 04 '19 at 12:06
  • Looking at @user3258569 answer [here](https://stackoverflow.com/questions/1429951/force-flushing-of-output-to-a-file-while-bash-script-is-still-running/15986937) you could try to spawn your program this way : `script -c -f`. It should force the flush on the `accoreconsole` program. [man of script](http://man7.org/linux/man-pages/man1/script.1.html) – Orelsanpls Oct 04 '19 at 12:07

0 Answers0