I'm trying to launch a command via NodeJS using the child_process.spawn() API. It seems to work fine if I use any command that is part of the system's environment path, but it throws an error if the command is not in the environment path. My application requires that it spawns a command process that is not, by default, part of the system environment. I want to make my code portable and so I would like to avoid forcing the end-user to add a particular command to their environment path.
I'm first developing on macOS:
Here's what works:
var cmd_directory = "/Applications/My Application Directory/"; //contains spaces
child = spawn('ls', ['-lh'], { cwd : cmd_directory }, function(err, stdout, stderr){
if (err) {
console.log("\t\tProblem spawning command =>\n\t\t" + err);
} else {
console.log("\t\tSpawned new process successfully!")
}
});
When running the above code, I get a directory listing of the /Applications/My Application Directory
.
Now, if I want to run a command that exists in that same directory, I get a ENOENT error. So, this code does not work: var cmd_directory = "/Applications/My Application Directory/"; //contains spaces
child = spawn('myapp', ['-v'], { cwd : cmd_directory }, function(err, stdout, stderr){
if (err) {
console.log("\t\tProblem spawning command =>\n\t\t" + err);
} else {
console.log("\t\tSpawned new process successfully!")
}
});
Why does this happen even though the working directory is set to the same location as where the command is?
Lastly, how can I make sure the myapp
command spawns even if it does not exist in the system's or user's environment path?
via ariestav
No comments:
Post a Comment