node.js child_process spawn repl - node.js

Nodejs Child Process: write to stdin from an already initialised process
I saw this link, so I try like this :
const { spawn } = require('child_process');
const child = spawn('node');
child.stdin.setDefaultEncoding('utf-8');
child.stdout.pipe(process.stdout);
child.stdin.cork();
child.stdin.write("10+20\n");
child.stdin.uncork();
but this code does not output anything, so what should I do?

Related

Why child_process.spawn prevents parent exit?

I wrote a python script containing infinite loop (called it test.py).
And then I run it using spawn in nodeJS.
Here is the content of the nodejs Script:
const {spawn} = require("child_process")
spawn("python", ["test.py"], { encoding: 'utf8'});
Why this doesn't exit immediately?
Does the spawn put something in the Event Table or Poll Queue?

node.js replace child_process.exec with spawn

this is my previous code:-
const child_process = require('child_process');
child_process.exec(`java -cp ./${dirPath}bin Main`);
I tried to replace this like below:-
let spawn = require('child_process').spawn;
let child = spawn('java', [`-cp ./${dirPath}bin Main`]);
but I got error :-
"options" argument must be an object
How can I use spawn to execute java file by giving a specific path?
This works without an error in Node 10 on Windows:
let spawn = require('child_process').spawn;
let child = spawn('java', ['-version']);
Of course, this code throws away all output.
Also, there is an error in arguments, so your code should look like
let spawn = require('child_process').spawn;
let child = spawn('java', ['-cp', `./${dirPath}bin`, 'Main']);
To Konstantin's answer --> please be aware, that child_process package contained malicious code and was removed from the registry by the npm security team.

Node Spawn process outliving the main node process

I want to start a process that will live on its own and continue to live even if the node application that started it dies.
To do so I am trying to use child_process and I did not manage to have the process live even if the node process die.
Here is my code:
const cp = require('child_process');
const process = cp.spawn('long_running_process', ['arg1'], {
stdio: 'ignore',
detached: true
});
process.unref();
This code follows the child_process documentation available here :
https://nodejs.org/api/child_process.html#child_process_options_detached

Spawn child_process on directory

How to spawn this command (/usr/bin/which flac) on node.js:
var spawn = require('child_process').spawn;
var cmd = spawn('/usr/bin/which flac', parameters);
I've tried that code but its not working, assuming that parameters variables are set.
In your case, flac needs to be passed as a parameter. Try this:
var spawn = require('child_process').spawn;
var cmd = spawn('/usr/bin/which', ['flac'], {detached:true, stdio: 'inherit'})
.on('exit',function(code){
//check exit code
});
For example, running the same code with node instead of flac gives:
/usr/bin/node

Nodejs Child Process: write to stdin from an already initialised process

I am trying to spawn an external process phantomjs using node's child_process and then send information to that process after it was initialized, is that possible?
I have the following code:
var spawn = require('child_process').spawn,
child = spawn('phantomjs');
child.stdin.setEncoding = 'utf-8';
child.stdout.pipe(process.stdout);
child.stdin.write("console.log('Hello from PhantomJS')");
But the only thing I got on the stdout is the initial prompt for phantomjs console.
phantomjs>
So it seems the child.stdin.write is not making any effect.
I am not sure I can send additional information to phantomjs ater the initial spawn.
You need to pass also \n symbol to get your command work:
var spawn = require('child_process').spawn,
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(); /// this call seems necessary, at least with plain node.js executable
You need to surround your write by cork and uncork, the uncork method flushes all data buffered since cork was called. child.stdin.end() will flush data too, but no more data accepted.
var spawn = require('child_process').spawn,
child = spawn('phantomjs');
child.stdin.setEncoding('utf-8');
child.stdout.pipe(process.stdout);
child.stdin.cork();
child.stdin.write("console.log('Hello from PhantomJS')\n");
child.stdin.uncork();

Resources