2017-09-27 88 views
1

我想使用nodejs在命令提示符下運行命令。
基於https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node,我使用exec vs execFile nodeJs

child_process.execFile('protractor', ['./src/convertedJs/tempProtractorconfig.js'], (err, stdout, stderr) => {} 

上面的代碼拋出一個ENOENT錯誤。
但是當我運行

child_process.exec('protractor ./src/convertedJs/tempProtractorconfig.js', (err,stdout,stderr) => {}` 

一切工作正常。
有人可以解釋發生了什麼?

在v9.5.0的NodeJS
+0

你確定你是從同一目錄中運行他們兩個? –

+0

yepss ..我在同一個目錄中運行 –

回答

0

,所述exec被定義爲在以下文件LIB/child_process.js

exports.exec = function(command /*, options, callback*/) { 
var opts = normalizeExecArgs.apply(null, arguments); 
return exports.execFile(opts.file, opts.options, opts.callback); 

};

這意味着,最後exec電話execFile到EXCUTE您的命令,所以你的情況很奇怪,execFile失敗,但exec不是