Running Node.js commands in Electron


#1

How do you execute a node.js command that is stored inside of a javascript file before electron runs?

I’ve seen this post but but I still can’t seem to understand how it works. Any help would be greatly appreciated.


#2

What is it you’re trying to do exactly?


#3

I have a batch file in windows that I need to initiate at the start of electron. I have the below command placed within a javascript file that works perfectly when run from the node command line. I was hoping I could initiate this file in a similar fashion in electron through it’s node api. Not sure how the syntax works however or where to put it. Thanks for the help

///Node Commmand Within JS File

var exec = require('child_process').execFile;

var fun =function(){
   console.log("fun() start");
   exec('test.bat', function(err, data) {  
        console.log(err)
        console.log(data.toString());                       
    });  
}
fun();

#4

This could be run in the main electron file or any JavaScript within the ui. It should work exactly the same. The exception may be if the file is wrapped in an asar.


#5

I’ve tried that without success. Maybe I am doing something wrong, could you explain where I need to put it within the main file? I’ve put it all over and am getting no success as well as within the UI as a clickable command or just a command that runs on open. All I get is the code showing but no execution. Thanks for all the help


#6

@joshual I was curious if you were able to get that working?


#7

exec receives a third argument which is the standard error. Perhaps you are having some information there that you are not seeing.