18

I'm building an electron app,

I can run shell commands pretty easily with the shell api (https://electronjs.org/docs/api/shell)

This command runs perfect for example:

shell.openItem("D:\test.bat");

This one does not

shell.openItem("D:\test.bat argument1");

How to run electron shell command with arguments?

1 Answer 1

22

shell.openItem isn't designed for that.
Use the spawn function of NodeJS from the child_process core module.

let spawn = require("child_process").spawn;

let bat = spawn("cmd.exe", [
    "/c",          // Argument for cmd.exe to carry out the specified script
    "D:\test.bat", // Path to your file
    "argument1",   // First argument
    "argumentN"    // n-th argument
]);

bat.stdout.on("data", (data) => {
    // Handle data...
});

bat.stderr.on("data", (err) => {
    // Handle error...
});

bat.on("exit", (code) => {
    // Handle exit
});
Sign up to request clarification or add additional context in comments.

4 Comments

Does it require node installed or just npm dependency?
I mean, is "child_process" only part of electron js ie: I don't need nodejs installed in client desktop ?
@SlimaneDeb child_process is part of NodeJS, not electron. It's a core module. See here.
@SlimaneDeb, to clarify NullDev's statement, electron by default bundles nodejs in its binary. So all core modules should be available to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.