1

I'm trying to get the following code which should execute shell command to wor in OSX and Windows.

const exec = require("child_process").exec;
const runCommand = (cmd) => exec(cmd,
    function (error, stdout, stderr) {
        if (stdout.length > 0) {
            console.log(stdout);
        }
    });

For the following runCommand("./node_modules/.bin/someBinary") it does not work in Windows. But it works fine in OSX. So I wonder what modifications do I need for it to run in Windows as well?

starcorn
  • 8,261
  • 23
  • 83
  • 124

2 Answers2

0

Have you considered giving a try at npm-exec ?

Loads the same environment that would be present for npm run-script , but executes arbitrary bash command lines. (even on windows! Thanks to bashful). This includes modifying $PATH so scripts in node_modules/.bin will be used before global modules.

So you could npm-exec someBinary instead of using a relative path breaking Windows / OSX compatibility

If this module does not suit you, you can also give a try at npm bin as explained in this answer.

Community
  • 1
  • 1
bviale
  • 5,245
  • 3
  • 28
  • 48
  • This does not directly solve your problem but I ran into the same issue while working on a gulp file. The most elegant solution I found was to get rid of gulp and use the "scripts" node of my package.json, there you can freely use the command line tools of your project dependencies as your path is automatically filled with the content of `./node_modules/**/.bin/` – bviale Mar 10 '17 at 15:13
  • It was same result with `npm bin`. Since I need to wrap it around in something `$(npm bin)/someBinary` and Windows does not recognize the dollar sign... – starcorn Mar 10 '17 at 15:20
  • @starcorn ok but what about npm-exec ? – bviale Mar 10 '17 at 15:24
  • I think there would be same issue with npm-exec. I guess I need to install npm-exec globally. Which isn't an option for me now. So in end I would also need to find its binary inside /node_modules/.bin/ :( – starcorn Mar 10 '17 at 15:26
0

Okay, so in the end I solved it with checking which platform I'm in and in turn I just wrote the same command with OS specific so it would run.

In short I used process.platform to find out which OS it is. Not the most elegant. But at least it solved my problem.

starcorn
  • 8,261
  • 23
  • 83
  • 124