Techioz Blog

Node.js を使用してコマンド ライン バイナリを実行する

概要

CLI ライブラリを Ruby から Node.js に移植しているところです。私のコードでは、必要に応じていくつかのサードパーティのバイナリを実行します。 Node でこれを実現する最善の方法がわかりません。

以下は、PrinceXML を呼び出してファイルを PDF に変換する Ruby の例です。

cmd = system("prince -v builds/pdf/book.html -o builds/pdf/book.pdf")

Node の同等のコードは何ですか?

解決策

Node.js のさらに新しいバージョン (v8.1.4) では、イベントと呼び出しは古いバージョンと類似または同一ですが、標準の新しい言語機能を使用することをお勧めします。例:

バッファリングされた非ストリーム形式の出力の場合 (すべてを一度に取得できます)、child_process.exec を使用します。

const { exec } = require('child_process');
exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }

  // the *entire* stdout and stderr (buffered)
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

Promise と一緒に使用することもできます。

const util = require('util');
const exec = util.promisify(require('child_process').exec);

async function ls() {
  const { stdout, stderr } = await exec('ls');
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);
}
ls();

データをチャンクで徐々に受信したい場合 (ストリームとして出力)、child_process.spawn を使用します。

const { spawn } = require('child_process');
const child = spawn('ls', ['-lh', '/usr']);

// use child.stdout.setEncoding('utf8'); if you want text chunks
child.stdout.on('data', (chunk) => {
  // data from standard output is here as buffers
});

// since these are streams, you can pipe them elsewhere
child.stderr.pipe(dest);

child.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

これらの関数には両方とも同期関数があります。 child_process.execSync の例:

const { execSync } = require('child_process');
// stderr is sent to stderr of parent process
// you can set options.stdio if you want it to go elsewhere
let stdout = execSync('ls');

child_process.spawnSync と同様に:

const { spawnSync} = require('child_process');
const child = spawnSync('ls', ['-lh', '/usr']);

console.log('error', child.error);
console.log('stdout ', child.stdout);
console.log('stderr ', child.stderr);

注: 次のコードはまだ機能しますが、主に ES5 以前のユーザーを対象としています。

Node.js で子プロセスを生成するモジュールについては、ドキュメント (v5.0.0) に詳しく記載されています。コマンドを実行し、その完全な出力をバッファとして取得するには、child_process.exec を使用します。

var exec = require('child_process').exec;
var cmd = 'prince -v builds/pdf/book.html -o builds/pdf/book.pdf';

exec(cmd, function(error, stdout, stderr) {
  // command output is in stdout
});

大量の出力が予想される場合など、ストリームでプロセス I/O を処理する必要がある場合は、child_process.spawn を使用します。

var spawn = require('child_process').spawn;
var child = spawn('prince', [
  '-v', 'builds/pdf/book.html',
  '-o', 'builds/pdf/book.pdf'
]);

child.stdout.on('data', function(chunk) {
  // output will be here in chunks
});

// or if you want to send output elsewhere
child.stdout.pipe(dest);

コマンドではなくファイルを実行している場合は、child_process.execFile を使用するとよいでしょう。このパラメータは spawn とほぼ同じですが、出力バッファを取得するための exec のような 4 番目のコールバック パラメータがあります。それは次のようになります。

var execFile = require('child_process').execFile;
execFile(file, args, options, function(error, stdout, stderr) {
  // command output is in stdout
});

v0.11.12 以降、Node は同期の生成と実行をサポートするようになりました。上で説明したメソッドはすべて非同期であり、対応する同期メソッドがあります。それらのドキュメントはここにあります。これらはスクリプト作成には便利ですが、子プロセスを非同期的に生成するために使用されるメソッドとは異なり、同期メソッドは ChildProcess のインスタンスを返さないことに注意してください。