Categories
JavaScript Answers

How to write to stdin from an already initialized process with Nodejs Child Process?

Spread the love

to write to stdin from an already initialized process with Nodejs Child Process, we call trhe stdin.write method.

For instance, we write

const spawn = require("child_process").spawn;
const child = spawn("phantomjs");

child.stdin.setEncoding("utf-8");
child.stdout.pipe(process.stdout);

child.stdin.write("console.log('Hello from PhantomJS')\n");

child.stdin.end();

to call stdin.write to write to stdin.

And then we stop writing with stdin.end.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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