Electron自动更新跨用户安装路径不一致问题解决方案咨询
Environment
- Electron: 29.3.0
- Electron-Builder: 24.13.3
- Electron-Updater: 6.2.1
Issue
When the app is installed via a temporary admin account, update files get stored in C:\Users\tempadmin\AppData\Local\crewworks-onboard-app-updater. When non-admin users run the app, electron-updater tries to download updates to their own user-specific C:\Users\[CurrentUser]\AppData\Roaming\crewworks-onboard-app directory. This breaks differential updates because the original installer isn't present in the current user's path. Manual copying of the installer to the user's directory works, but this isn't a sustainable fix for multi-user setups.
1. Force a Consistent Cross-User Update Path
To make electron-updater use a shared path accessible by all users, override the default cache and download directories in your config or runtime code.
Option 1: Electron-Builder Config
Add updaterCacheDirName to your build config (in package.json or electron-builder.yml) to point to a system-wide directory:
"build": { "win": { "target": "nsis", "publish": { "provider": "generic", "url": "your-update-server-url" } }, "updaterCacheDirName": "C:\\ProgramData\\crewworks-onboard-app-updater" }
Important: Ensure the ProgramData directory has read/write permissions for all users. You can set this during installation by modifying the NSIS script to grant access to the "Users" group.
Option 2: Runtime Override
In your main process, before initializing autoUpdater, explicitly set the download folder and config path:
const { autoUpdater } = require('electron-updater'); // Set shared update directory autoUpdater.downloadsFolder = 'C:\\ProgramData\\crewworks-onboard-app-updater'; autoUpdater.updateConfigPath = `${autoUpdater.downloadsFolder}\\dev-app-update.yml`;
2. Admin-Install, Non-Admin Use Update Management
Per-Machine Installation
Configure your NSIS installer to install for all users instead of just the admin. This stores the app in C:\Program Files and uses a system-wide update cache:
"build": { "nsis": { "perMachine": true, "allowElevation": true } }
This is the cleanest solution because it eliminates user-specific paths entirely. The installer will prompt for admin rights during installation, and all users can run the app without path conflicts.
Elevated Update Execution
If per-machine installation isn't an option, modify the updater to run with admin privileges when applying updates. You can use Electron's shell.openItem to launch the installer with elevation, or create a small helper executable that requests admin rights to handle the update installation across users.
Centralized Cache
Use a shared network drive or system-wide local directory as the update cache. During installation, set NTFS permissions on this directory to allow all users read/write access. This ensures every user uses the same cache location for updates.
3. Update Path Configuration & User Directory Guidance
- Default Path Behavior: Electron-updater uses user-specific directories by default. For per-user installs, it uses
AppData\Local\<app-name>-updater; for per-machine installs (withperMachine: true), it usesProgramData\<app-name>-updater. Knowing these defaults helps you override them effectively. - Permission Criticality: When using shared directories, always set proper permissions. For Windows, grant read/write access to the "Users" group to avoid access denied errors for non-admin users.
- Avoid Hardcoding: Use environment variables like
process.env.PROGRAMDATA(Windows) orprocess.env.HOME(macOS/Linux) instead of hardcoding paths. This makes your config more portable and OS-agnostic.
内容的提问来源于stack exchange,提问作者Frankenstein

