Execute a command line binary with Node.js

后端 未结 12 2510
星月不相逢
星月不相逢 2020-11-22 01:39

I am in the process of porting a CLI library from Ruby over to Node.js. In my code I execute several third party binaries when necessary. I am not sure how best to accomplis

12条回答
  •  半阙折子戏
    2020-11-22 02:16

    You are looking for child_process.exec

    Here is the example:

    const exec = require('child_process').exec;
    const child = exec('cat *.js bad_file | wc -l',
        (error, stdout, stderr) => {
            console.log(`stdout: ${stdout}`);
            console.log(`stderr: ${stderr}`);
            if (error !== null) {
                console.log(`exec error: ${error}`);
            }
    });
    

提交回复
热议问题