Emjey
Emjey

Reputation: 2063

How to Execute a command through electron app

I am trying to execute a command on the click of an element in the electron app.

The code is:

function displayadbinfo(command) {
    const childProcess = require('child_process');
    alert(childProcess.execSync(command).toString());
}

This code pops up an alert when I click on the app which is launched with npm start. However, after building the executable file, the command returns an empty response.

Upvotes: 2

Views: 2888

Answers (1)

Akshay Anurag
Akshay Anurag

Reputation: 744

child_process.execSync returns a buffer or a string rather than a stream, which could be causing an empty response. It waits for the child process to exit and tries to return all the buffered data at once. I suggest you to use child_process.spawn.

The difference is that child_process.spawn returns a ChildProcess object and stdout and stderr are accessible using streams rather than synchronously returned as a buffer. So, you'd be able to see the output correctly.

I am not sure if electron-builder or electron-packager might be causing issues.

You can find an article discussing the differences between spawn () and exec () here on hacksparrow.com

Upvotes: 1

Related Questions