exec vs execFile nodeJs
Asked Answered
A

3

12

I want to run a command in command-prompt using nodejs.
Based on https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node, i used

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

The above code throws a ENOENT error.
But when i run

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

everything works fine.
Can somebody explain what is happening?

Adjustment answered 27/9, 2017 at 10:41 Comment(2)
Are you sure you are running both of them from the same directory?Nathanielnathanil
yepss..i am running in the same dirAdjustment
C
14

There is a difference between using child_process.exec() and child_process.execFile() in that the latter won't spawn a shell whereas the former will.

Nodejs documentation states:

On Windows, however, .bat and .cmd files are not executable on their own without a terminal, and therefore cannot be launched using child_process.execFile(). When running on Windows, .bat and .cmd files can be invoked using child_process.spawn() with the shell option set, with child_process.exec(), or by spawning cmd.exe and passing the .bat or .cmd file as an argument (which is what the shell option and child_process.exec() do).

I could launch them... though not without problem.

My observations:

  • Running the following child_process.execFile('ls', ...) works on Linux whereas child_process.execFile('dir', ...) doesn't work on Windows.
  • specifying the fullpath to the protractor executable on Windows, e.g. child_process.execFile('C:\\Users\\Jperl\\AppData\\Roaming\\npm\\protractor.cmd', ...) works as expected! No shell means we don't have access to the path variable.

Don't use execFile for Windows at all. Instead use either spawn or exec:

var protractor = child_process.spawn('protractor', ['./src/convertedJs/tempProtractorconfig.js'], {shell: true});

or

var protractor = child_process.spawn('cmd', ['/c', 'protractor', './src/convertedJs/tempProtractorconfig.js']);
Cauvery answered 5/8, 2019 at 16:1 Comment(4)
Is this still correct? Looking at the Node.js documentation I see explicit mention of how to use this function on Windows - nodejs.org/api/…Intake
@ZachSmith I think so, all the things I said are still there nodejs.org/api/… The second part is based on my own observations back then.Cauvery
Should still be correct. It's quite easy to test, though.Kianakiang
Is exec( the same as execFile with shell option set to true ?Copulative
G
2

in nodejs v9.5.0, the exec is defined as below in file 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);
};

that means ,finally exec call execFile to excute your command , so in your case it quite strange that execFile fail but exec is not

Grous answered 2/3, 2018 at 7:14 Comment(0)
D
0

I also faced some issue to start a node process by using "node myfile.js",but when I added the location of the node.exe like 'path of node.exe/node.exe myfile.js ' it was working fine like I expected

Daughtry answered 1/12, 2021 at 3:48 Comment(1)
I forgot to add onething,maybe try adding the file path of protractor will help you.Daughtry

© 2022 - 2024 — McMap. All rights reserved.