1

I have a c program (I didn't code it) that prints some data in the terminal. I launch the program as a child process in node with the spawn function.

const child_process = spawn('./myProgram', ['--arg']);

After that, I code the event to get the printed data:

child_process.stdout.on('data', function(data) {
        console.log(data);
});

When I run the program I can't see the output data from my c program in my nodejs terminal. If I initialize the child process with stdio as inherit it works.

const child_process = spawn('./myProgram', ['--arg'], {stdio :'inherit'});

The key point here is that I need to process that data in my nodejs app. I suppose the way the c file prints the data is not the standard one, so my nodjs program does not get it.

7
  • Shouldn't that be child_process.stdout.on(...)? Commented Aug 13, 2020 at 0:56
  • Yes, you are right. I missed out typing the question. Commented Aug 13, 2020 at 1:01
  • Does this work with other programs? A simple test is like spawn('ls'). Commented Aug 13, 2020 at 1:02
  • Another thing to check is that you're not getting any errors, or that it's not outputting to stderr instead. Commented Aug 13, 2020 at 1:03
  • It works. The problem is with that c program. I have also created my own c program to test it and works. Commented Aug 13, 2020 at 1:04

1 Answer 1

1

The file was outputting to stderr instead to stdout. It was fixed by adding the event to stderr:

child_process.stderr.on('data', function(data) {
        console.log(data);
});

@tadman got the answere.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.