1

I want to execute a homebrew command for example

brew list

I followed the documentation and executed it like this:

child = exec('brew', function (error, stdout, stderr) {
 console.log(stdout);
 console.log(stderr);
});

I am getting a command not found error, and realized that if I do /usr/local/bin/brew as the command it works. However simply using 'brew' should work as well since I can run 'brew' from the command line just as such.

Why is this the case and what does it take to make 'brew' run as a child process in node? I have a feeling part of the issue because the command on node-webkit seems to execute from bin/sh.

Thanks

user2554585
  • 3,489
  • 6
  • 20
  • 23

1 Answers1

1

It may depend on how you're starting node-webkit and how you're setting your PATH. When I start from the command line, it inherits the environment variables from my command-line environment, including PATH. If I start by double clicking in a gui, it inherits from the system (presumably /etc/paths), and any additions I make in my .bashrc/.bash_profile have no effect.

Also, I'm no security expert, but my understanding of best practices would include using an absolute path to the executable you're running, so it's harder to spoof by setting an environment variable. By that measure, you're better off using the full path to brew anyway.

OldGeeksGuide
  • 2,888
  • 13
  • 23
  • Thanks, there is indeed a difference between launching from the command line versus double clicking. The question is it clearly would be better if I can just simply double click the app. Any insights as to how I may achieve that? – user2554585 Oct 02 '14 at 23:10
  • Well, it depends on what you're trying to achieve. Do you mean that you need to have access to brew? How do you know that your users have brew installed at all? – OldGeeksGuide Oct 02 '14 at 23:16
  • As per your answer, I launched from the command line and it works perfectly fine. It's just when I launch by double-clicking that the environment variable seems to be missing. I also install brew as part of my app. – user2554585 Oct 02 '14 at 23:21
  • Ah, I see. Then I would use the full absolute path to brew, it should work in either case. Since you install brew as part of your app, I assume you can be sure where it installs. – OldGeeksGuide Oct 02 '14 at 23:51
  • There is a problem. Let's **assume** brew calls another command (like my-command) which is in a PATH set perhaps by your .bash_profile file or something. Then the brew command will fail because it can't find my-command. Using the absolute path to brew will not solve this issue. The only option is to pass a PATH variable with **env** option when calling exec. – Thanish Dec 11 '14 at 16:27