frank hk
frank hk

Reputation: 127

Run an Executable by nodejs

I want to run an executable by using nodeJS.Followings are my tries to achieve this.

MY Try:

main.js

var subpy = require('child_process').execFile("C:\\datajr\\datajr.exe");

2nd Try:

I referred some QA in stack from there i got a solution like below:

var child = require('child_process').execFile;
  var executablePath = "C:\\datajr\\datajr.exe";

   child(executablePath, function(err, data) {
        if(err){
           console.error(err);
           return;
        }

    console.log(data.toString());
});

Upvotes: 0

Views: 3208

Answers (2)

Boo
Boo

Reputation: 189

I suppose you want to run the electron app and another executable at the same time, in that case you can use concurrently. Suppose you can execute the executable directly from the commandline like datajr then you can modify your package.json by adding :

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "concurrently \"datajr\" \"electron main.js\" ",
  }

Now npm start to start the electron app and the executable. In this way you can overcome the hustle of executing the file from within the main.js.

Upvotes: 2

0bero
0bero

Reputation: 1062

From the npm Documentation:

npm start runs an arbitrary command specified in the package's "start" property of its "scripts" object (in your package.json file). If no "start" property is specified on the "scripts" object, it will run node server.js.

So, if you don't have a package.json in the directory of your project or it doesn't include your main.js in the start property of scripts, npm start won't work.

If you want to know more about this, check the npm documentation:

For npm start: https://docs.npmjs.com/cli/start

For the package.json file: https://docs.npmjs.com/files/package.json

Upvotes: 0

Related Questions