Run a subprocess with administrative privileges, prompting the user with a graphical OS dialog if necessary. Useful for background subprocesse which run native Electron apps that need sudo.
Uses in-built OS features, no binaries required
- Supports
spawn
andexec
subprocess behavior - Supports applications packaged as
asar
archive - Separate password prompt for each call (use
sh
orbat
script for single prompt) - No external dependencies, does not depend on OS versions
npm install david-mohr/electron-sudo
Note: Your command should not start with the sudo
prefix.
import sudo from 'electron-sudo';
/* Spawn subprocess behavior */
let cp = await sudo.spawn(
'echo', ['$PARAM'], {env: {PARAM: 'VALUE'}}
);
cp.on('close', () => {
/*
cp.output.stdout (Buffer)
cp.output.stderr (Buffer)
*/
});
/* Exec subprocess behavior */
let result = await sudo.exec(
'echo $PARAM', {env: {PARAM: 'VALUE'}}
);
/* result is an object with props stdout and stderr (both Buffers) */
npm i && npm test
Based on the original electron-sudo
by Aleksandr Komlev