Running a shell command from Node.js without buffering output

前端 未结 4 686
傲寒
傲寒 2021-01-30 10:37

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

4条回答
  •  情歌与酒
    2021-01-30 11:03

    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}.

提交回复
热议问题