From ee3c522a3725dd82511b2b9b5589cf0598e5cf6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=BB=E5=8B=87=E7=A5=A5?= <237899745@qq.com> Date: Wed, 12 Nov 2025 12:00:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20=E6=94=B9=E8=BF=9Bnpm?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=AE=89=E8=A3=85=EF=BC=8C=E8=A7=A3=E5=86=B3?= =?UTF-8?q?better-sqlite3=E7=BC=96=E8=AF=91=E5=A4=B1=E8=B4=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题分析: - better-sqlite3是native模块,需要node-gyp编译 - node-gyp需要Python环境 - 部分系统只有python3命令,没有python命令 - 导致编译失败: "python3" is not in PATH or produced an error 修复内容: 1. 在安装和更新依赖前自动创建python软链接(python -> python3) 2. 配置npm使用python3: npm config set python python3 3. 添加依赖安装进度提示(避免用户以为卡住) 4. 改进错误处理和提示信息 5. 安装失败时提供详细的解决方案和日志路径 6. 支持用户选择忽略错误继续安装 适用场景: - Ubuntu/Debian: 使用ln -sf创建软链接 - CentOS/RHEL: 使用alternatives管理Python版本 - 其他系统: 回退到ln -sf 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- install.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index 2c50c4a..f69a370 100644 --- a/install.sh +++ b/install.sh @@ -1081,14 +1081,52 @@ install_backend_dependencies() { cd "${PROJECT_DIR}/backend" + # 确保Python可用(node-gyp需要) + if ! command -v python &> /dev/null; then + if command -v python3 &> /dev/null; then + # 创建python软链接指向python3 + if [[ "$OS" == "ubuntu" ]] || [[ "$OS" == "debian" ]]; then + ln -sf /usr/bin/python3 /usr/bin/python || true + else + # CentOS/RHEL/其他系统 + alternatives --install /usr/bin/python python /usr/bin/python3 1 &> /dev/null || \ + ln -sf /usr/bin/python3 /usr/bin/python || true + fi + print_info "已配置Python环境(python -> python3)" + else + print_warning "未找到Python,某些依赖可能需要手动处理" + fi + fi + # 使用国内镜像加速 if [[ "$USE_ALIYUN_MIRROR" == "true" ]]; then npm config set registry https://registry.npmmirror.com fi - npm install --production + # 设置npm使用Python3 + npm config set python python3 + + print_info "正在安装依赖包(包含数据库native模块,可能需要几分钟)..." + + # 安装依赖,捕获错误 + if npm install --production; then + print_success "后端依赖安装完成" + else + print_error "依赖安装失败" + echo "" + print_warning "可能的解决方案:" + echo " 1. 检查网络连接" + echo " 2. 手动执行: cd ${PROJECT_DIR}/backend && npm install --production" + echo " 3. 查看详细错误日志: ~/.npm/_logs/" + echo "" + + # 询问是否继续 + read -p "是否忽略错误继续安装?(y/n): " continue_install < /dev/tty + if [[ "$continue_install" != "y" ]] && [[ "$continue_install" != "Y" ]]; then + exit 1 + fi + fi - print_success "后端依赖安装完成" echo "" } @@ -1965,6 +2003,19 @@ update_install_dependencies() { cd "${PROJECT_DIR}/backend" + # 确保Python可用(node-gyp需要) + if ! command -v python &> /dev/null; then + if command -v python3 &> /dev/null; then + if [[ "$OS" == "ubuntu" ]] || [[ "$OS" == "debian" ]]; then + ln -sf /usr/bin/python3 /usr/bin/python || true + else + alternatives --install /usr/bin/python python /usr/bin/python3 1 &> /dev/null || \ + ln -sf /usr/bin/python3 /usr/bin/python || true + fi + print_info "已配置Python环境" + fi + fi + # 使用国内镜像加速(如果之前选择了) if command -v npm &> /dev/null; then current_registry=$(npm config get registry) @@ -1973,18 +2024,26 @@ update_install_dependencies() { fi fi + # 设置npm使用Python3 + npm config set python python3 + # 清理旧的node_modules if [[ -d "node_modules" ]]; then print_info "清理旧依赖..." rm -rf node_modules package-lock.json fi - npm install --production + print_info "正在重新安装依赖(可能需要几分钟)..." + + if npm install --production; then + print_success "依赖更新完成" + else + print_error "依赖更新失败" + print_warning "请检查错误日志: ~/.npm/_logs/" + fi - print_success "依赖更新完成" echo "" } - update_migrate_database() { print_step "迁移数据库配置..."