@expo/sudo-prompt

中文文档
9.3.2发布于 2025/01/23MITTypeScriptCommonJS零依赖近 30 天下载 5568.2万

Run a command using sudo, prompting the user with an OS dialog if necessary

原始使用文档
文档目录7 节
  1. Cross-Platform
  2. Installation
  3. Usage
  4. Behavior
  5. Non-graphical terminal commands only
  6. Concurrency
  7. Invalidating the timestamp

sudo-prompt

[!NOTE] This is a fork at expo/sude-prompt to resolve deprecated Node APIs. Upstreams:

Run a non-graphical terminal command using sudo, prompting the user with a graphical OS dialog if necessary. Useful for background Node.js applications or native Electron apps that need sudo.

Cross-Platform

sudo-prompt provides a native OS dialog prompt on macOS, Linux and Windows.

macOS

Linux

Windows

Installation

sudo-prompt has no external dependencies and does not require any native bindings.

npm install sudo-prompt

Usage

Note: Your command should not start with the sudo prefix.

var sudo = require('sudo-prompt');
var options = {
  name: 'Electron',
  icns: '/Applications/Electron.app/Contents/Resources/Electron.icns', // (optional)
};
sudo.exec('echo hello', options,
  function(error, stdout, stderr) {
    if (error) throw error;
    console.log('stdout: ' + stdout);
  }
);

sudo-prompt will use process.title as options.name if options.name is not provided. options.name must be alphanumeric only (spaces are supported) and at most 70 characters.

sudo-prompt will preserve the current working directory on all platforms. Environment variables can be set explicitly using options.env.

sudo-prompt.exec() is different to child-process.exec() in that no child process is returned (due to platform and permissions constraints).

Behavior

On macOS, sudo-prompt should behave just like the sudo command in the shell. If your command does not work with the sudo command in the shell (perhaps because it uses > redirection to a restricted file), then it may not work with sudo-prompt. However, it is still possible to use sudo-prompt to get a privileged shell, see this closed issue for more information.

On Linux, sudo-prompt will use either pkexec or kdesudo to show the password prompt and run your command. Where possible, sudo-prompt will try and get these to mimic sudo. Depending on which binary is used, and due to the limitations of some binaries, the name of your program or the command itself may be displayed to your user. sudo-prompt will not use gksudo since gksudo does not support concurrent prompts. Passing options.icns is currently not supported by sudo-prompt on Linux. Patches are welcome to add support for icons based on polkit.

On Windows, sudo-prompt will elevate your command using User Account Control (UAC). Passing options.name or options.icns is currently not supported by sudo-prompt on Windows.

Non-graphical terminal commands only

Just as you should never use sudo to launch any graphical applications, you should never use sudo-prompt to launch any graphical applications. Doing so could cause files in your home directory to become owned by root. sudo-prompt is explicitly designed to launch non-graphical terminal commands. For more information, read this post.

Concurrency

On systems where the user has opted to have tty-tickets enabled (most systems), each call to exec() will result in a separate password prompt. Where tty-tickets are disabled, subsequent calls to exec() will still require a password prompt, even where the user's sudo timestamp file remains valid, due to edge cases with sudo itself, see this discussion for more information.

You should never rely on sudo-prompt to execute your calls in order. If you need to enforce ordering of calls, then you should explicitly order your calls in your application. Where your commands are short-lived, you should always queue your calls to exec() to make sure your user is not overloaded with password prompts.

Invalidating the timestamp

On macOS and Linux, you can invalidate the user's sudo timestamp file to force the prompt to appear by running the following command in your terminal:

$ sudo -k

版本与发布记录

@expo/sudo-prompt 共发布 1 个版本,首次发布于 2025/01/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
9.3.2latest2025/01/231.8 年前

关于 @expo/sudo-prompt 的常见问题

@expo/sudo-prompt 是什么?

Run a command using sudo, prompting the user with an OS dialog if necessary

如何安装 @expo/sudo-prompt?

在项目目录运行 npm install @expo/sudo-prompt,或使用 pnpm add / yarn add / bun add。

@expo/sudo-prompt 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@expo/sudo-prompt 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@expo/sudo-prompt 可以商用吗?

@expo/sudo-prompt 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@expo/sudo-prompt 的下载量有多少?

根据 npm 官方下载统计,@expo/sudo-prompt 在 2026/10/14 – 2026/11/12 这 30 天内被下载 55,681,588 次,日均约 1,856,053 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@expo/sudo-prompt 还在维护吗?

最新版本 9.3.2 发布于 2025/01/23(1.8 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。