I'm trying to launch a shell command from Node.js, without redirecting that command's input and output -- just like shelling out to a command using a shell script, or using Ruby's system
command. If the child process wants to write to STDOUT, I want that to go straight to the console (or get redirected, if my Node app's output was redirected).
Node doesn't seem to have any straightforward way to do this. It looks like the only way to run another process is with child_process
, which always redirects the child process's input and output to pipes. I can write code to accept data from those pipes and write it to my process's STDOUT and STDERR, but if I do that, the APIs force me to sacrifice some flexibility.
I want two features:
It looks like Node wants to force me choose between those two features.
child_process.spawn
and then do child.stdout.on('data', function(data) { process.stdout.write(data); });
and the same thing for stderr
, and it'll happily pipe data until the cows come home. Unfortunately, spawn
doesn't support shell syntax.child_process.exec
. But exec
insists on buffering the child process's STDOUT and STDERR for me and giving them to me all at the end, and it limits the size of those buffers (configurable, 200K by default). I can still hook the on('data')
events, if I want to see the output as it's generated, but exec
will still add the data to its buffers too. When the amount of data exceeds the predefined buffer size, exec
will terminate the child process.(There's also child_process.execFile
, which is the worst of both worlds from a flexibility standpoint: no shell syntax, but you still have to cap the amount of output you expect.)
Am I missing something? Is there any way to just shell out to a child process in Node, and not redirect its input and output? Something that supports shell syntax and doesn't crap out after a predefined amount of output, just like is available in shell scripts, Ruby, etc.?
Node. js can run shell commands by using the standard child_process module. If we use the exec() function, our command will run and its output will be available to us in a callback. If we use the spawn() module, its output will be available via event listeners.
The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() . Let's begin creating our first child processes in Node.
You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.
Node. js files must be initiated in the "Command Line Interface" program of your computer. How to open the command line interface on your computer depends on the operating system. For Windows users, press the start button and look for "Command Prompt", or simply write "cmd" in the search field.
You can inherit stdin/out/error streams via spawn
argument so you don't need to pipe them manually:
var spawn = require('child_process').spawn; spawn('ls', [], { stdio: 'inherit' });
Use shell for shell syntax - for bash it's -c
parameter to read script from string:
var spawn = require('child_process').spawn; var shellSyntaxCommand = 'ls -l | grep test | wc -c'; spawn('sh', ['-c', shellSyntaxCommand], { stdio: 'inherit' });
To summarise:
var spawn = require('child_process').spawn; function shspawn(command) { spawn('sh', ['-c', command], { stdio: 'inherit' }); } shspawn('ls -l | grep test | wc -c');
You can replace exec
by spawn
and use the shell syntax simply with:
const {spawn} = require ('child_process'); const cmd = 'ls -l | grep test | wc -c'; const p = spawn (cmd, [], {shell: true}); p.stdout.on ('data', (data) => { console.log (data.toString ()); });
The magic is just {shell: true}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With