From 46cd8ae01eb94803c9b079637e5f48f533742e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E5=A4=A9?= Date: Mon, 20 Apr 2026 16:01:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20=E4=BF=AE=E5=A4=8D=20Linux=20?= =?UTF-8?q?=E5=85=A8=E6=96=B0=E7=B3=BB=E7=BB=9F=E9=83=A8=E7=BD=B2=20npm=20?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=9D=83=E9=99=90=E9=97=AE=E9=A2=98=20(#236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:全新 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 即清理) --- scripts/linux-deploy.sh | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/scripts/linux-deploy.sh b/scripts/linux-deploy.sh index bd1e664..2c9caf8 100644 --- a/scripts/linux-deploy.sh +++ b/scripts/linux-deploy.sh @@ -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 "📦 构建生产版本..."