Electron subprocess with administrative privileges
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.
Windows
, uses elevate utility with nativeUser Account Control (UAC)
prompt (noPowerShell
required)OS X
, uses bundled applet (inspired by Joran Dirk Greef)Linux
, uses systempkexec
or gksudo (system or bundled).
If you don't trust binaries bundled in npm
package you can manually build tools and use them instead.



Features
- 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
Installation
npm install electron-sudo
Usage
Note: Your command should not start with the sudo
prefix.
Version 4.0.*
; let options = name: 'electron sudo application' sudoer = options; /* Spawn subprocess behavior */let cp = await sudoer;cp; /* Exec subprocess behavior */let result = await sudoer;/* result is Buffer with mixed (both stdout and stderr) output */ /* Usage with Vanila JS */ var Sudoer = default;var sudoer = options;sudoer;
Version 3.0.* (deprecated)
var sudo = ;var options = name: 'Your application name' icns: '/path/to/icns/file' // (optional, only for MacOS), process: options: // Can use custom environment variables for your privileged subprocess env: 'VAR': 'VALUE' // ... and all other subprocess options described here // https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback { psstdout; ; } ;sudo;
Tests
npm i && npm test
Usage with Webpack
Webpack config should contain __dirname
equals true
for work properly
let nodeModules = fs ; ... target: 'electron' node: /* http://webpack.github.io/docs/configuration.html#node */ __dirname: true externals: nodeModules;