LoginSignup
9
7

More than 5 years have passed since last update.

nodeのchild_processのexecが失敗する時の対策

Last updated at Posted at 2016-04-13

child_processのexecの引数にはcwdを指定するとうまくいくことがあるという話です。

例えばWindowsでC:¥somewhare内のfoo.exeを呼び出したいとして、

app.js
var exec = require('child_process').exec;
exec('foo bar', function(error, stdout, stderr) {
    if (error != null) {
        console.log(error);
    }
});

というスクリプトを記述して C:¥somewhare¥app.jsのパスに保存して、コマンドプロンプトから

cd c:¥somewhare
node app.js

のようにして実行した時に、

Error: Command failed: C:\Windows\system32\cmd.exe /s /c "foo bar"

というエラーが出力された場合、以下の様にexecの引数に{cwd: 'C:¥somewhare'}を追加する。

app.js
var exec = require('child_process').exec;
exec('foo bar', {cwd: 'C:¥somewhare'}, function(error, stdout, stderr) {
    if (error != null) {
        console.log(error);
    }
});
9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7