修复: 改进npm依赖安装,解决better-sqlite3编译失败问题

问题分析:
- 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 <noreply@anthropic.com>
This commit is contained in:
2025-11-12 12:00:57 +08:00
parent c272724c5c
commit ee3c522a37

View File

@@ -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
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
echo ""
}
update_migrate_database() {
print_step "迁移数据库配置..."