fix(deploy): 修复 Linux 全新系统部署 npm 缓存权限问题 (#236)

现象:全新 Ubuntu 上执行一键部署脚本后,npm install 报大量
TAR_ENTRY_ERROR / ENOTEMPTY / EACCES 错误,node_modules 处于半损坏状态,
导致后续 vite build 找不到 picomatch 等依赖。

根因:OpenClaw 安装阶段使用 sudo -E npm install -g 安装全局包时,
会在用户主目录 ~/.npm 缓存目录里留下 root 拥有的文件,
后续用户态的 npm install 无法写缓存 → 安装中断 → node_modules 残缺。

修复:
- 新增 fix_npm_cache_permissions:检测 ~/.npm 中是否存在 root 拥有的文件,
  自动执行 chown 恢复当前用户权限(root/普通用户分别走 chown 与 sudo chown)
- install_clawpanel 调用一次权限预检,并对 npm install 失败场景增加一次
  清理 node_modules 后的重试,避免半损坏目录污染构建
- 升级路径里补充对不完整 node_modules 的探测(缺少 .package-lock.json 即清理)
This commit is contained in:
晴天
2026-04-20 16:01:43 +08:00
parent 1ef9ca8ede
commit 46cd8ae01e

View File

@@ -203,13 +203,43 @@ install_openclaw() {
fi
}
# 修复 npm 缓存权限(曾用 sudo npm install 导致缓存被 root 拥有)
fix_npm_cache_permissions() {
local npm_cache_dir="$HOME/.npm"
if [ -d "$npm_cache_dir" ]; then
# 检查是否存在 root 拥有的文件
local root_files=$(find "$npm_cache_dir" -uid 0 2>/dev/null | head -1)
if [ -n "$root_files" ]; then
echo "⚠️ 检测到 npm 缓存中存在 root 权限文件,正在修复..."
if [ "$IS_ROOT" = true ]; then
chown -R "$(stat -c '%u:%g' "$HOME")" "$npm_cache_dir" 2>/dev/null || true
else
sudo chown -R "$(id -u):$(id -g)" "$npm_cache_dir" 2>/dev/null || true
fi
echo "✅ npm 缓存权限已修复"
fi
fi
}
# 克隆并安装 ClawPanel
install_clawpanel() {
# 预检 npm 缓存权限(#236: 全新系统部署时 npm cache 可能被 root 污染)
fix_npm_cache_permissions
if [ -d "$INSTALL_DIR" ] && [ -f "$INSTALL_DIR/package.json" ]; then
echo "📦 ClawPanel 已存在,更新中..."
cd "$INSTALL_DIR"
git pull origin main 2>/dev/null || true
npm install --registry "$NPM_REGISTRY"
# 清理可能损坏的 node_modules上次 npm install 失败残留)
if [ -d "node_modules" ] && [ ! -f "node_modules/.package-lock.json" ]; then
echo "⚠️ 检测到不完整的 node_modules清理后重新安装..."
rm -rf node_modules
fi
npm install --registry "$NPM_REGISTRY" || {
echo "⚠️ npm install 失败,清理 node_modules 后重试..."
rm -rf node_modules
npm install --registry "$NPM_REGISTRY"
}
else
echo "📦 克隆 ClawPanel..."
mkdir -p "$INSTALL_DIR"
@@ -218,7 +248,11 @@ install_clawpanel() {
git clone "$REPO_URL_GITEE" "$INSTALL_DIR"
fi
cd "$INSTALL_DIR"
npm install --registry "$NPM_REGISTRY"
npm install --registry "$NPM_REGISTRY" || {
echo "⚠️ npm install 失败,清理 node_modules 后重试..."
rm -rf node_modules
npm install --registry "$NPM_REGISTRY"
}
fi
# 生产构建(生成优化后的静态文件)
echo "📦 构建生产版本..."