#!/bin/bash # n8n 优化版启动脚本 - 解决所有警告 # 作者: 小齐 # 最后更新: 2025-09-11 set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # 打印函数 print_info() { echo -e "${BLUE}[信息]${NC} $1"; } print_success() { echo -e "${GREEN}[成功]${NC} $1"; } print_warning() { echo -e "${YELLOW}[警告]${NC} $1"; } print_error() { echo -e "${RED}[错误]${NC} $1"; } # 显示启动横幅 show_banner() { echo -e "${BLUE}" echo "╔══════════════════════════════════════╗" echo "║ n8n 中文版优化启动脚本 ║" echo "║ 版本: n8n-1.109.2 (无警告版) ║" echo "║ 维护者: xiaoqi ║" echo "╚══════════════════════════════════════╝" echo -e "${NC}" } # 修复配置文件权限 fix_permissions() { print_info "检查配置文件权限..." # 检查并修复 .n8n 目录权限 if [ -d "$HOME/.n8n" ]; then if [ -f "$HOME/.n8n/config" ]; then chmod 600 "$HOME/.n8n/config" print_success "配置文件权限已修复" fi else mkdir -p "$HOME/.n8n" touch "$HOME/.n8n/config" chmod 600 "$HOME/.n8n/config" print_success "创建并设置了配置文件权限" fi } # 检查依赖 check_dependencies() { print_info "正在检查系统依赖..." if ! command -v node &> /dev/null; then print_error "Node.js 未安装或未在 PATH 中" exit 1 fi if ! command -v pnpm &> /dev/null; then print_error "pnpm 未安装或未在 PATH 中" print_info "请先安装 pnpm: npm install -g pnpm" exit 1 fi print_success "依赖检查通过" } # 检查端口占用 check_port() { local port=5678 if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then print_warning "端口 $port 已被占用" print_info "正在查看占用进程..." lsof -i :$port | head -5 read -p "是否杀死占用进程?(y/N): " kill_process if [[ $kill_process =~ ^[Yy]$ ]]; then print_info "正在终止占用进程..." lsof -ti:$port | xargs kill -9 2>/dev/null || true sleep 1 print_success "进程已终止" else print_error "无法启动 n8n,端口被占用" exit 1 fi fi } # 停止现有的 n8n 进程 stop_existing() { print_info "正在停止现有的 n8n 进程..." pkill -f "pnpm.*dev" 2>/dev/null || true pkill -f "pnpm.*start" 2>/dev/null || true pkill -f "turbo.*dev" 2>/dev/null || true pkill -f "n8n" 2>/dev/null || true sleep 2 print_success "已停止现有进程" } # 设置环境变量 setup_environment() { print_info "正在设置优化的环境变量..." # 获取局域网IP地址 local lan_ip="" if [[ "$OSTYPE" == "darwin"* ]]; then lan_ip=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | head -1 | awk '{print $2}') else lan_ip=$(hostname -I | awk '{print $1}') fi # 基础配置 export N8N_DEFAULT_LOCALE=zh-CN export N8N_HOST=0.0.0.0 export N8N_PORT=5678 # 权限配置 - 解决权限警告 export N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true # 安全配置 export N8N_SECURE_COOKIE=false export N8N_SESSION_COOKIE_SAME_SITE=lax export N8N_SESSION_COOKIE_SECURE=false # 性能优化 export DB_SQLITE_POOL_SIZE=5 export N8N_RUNNERS_ENABLED=true export N8N_BLOCK_ENV_ACCESS_IN_NODE=false # Node.js 优化 - 避免废弃警告 export NODE_NO_WARNINGS=1 export NODE_OPTIONS="--no-deprecation" # 设置URL if [ -n "$lan_ip" ]; then export N8N_EDITOR_BASE_URL="http://${lan_ip}:5678" export WEBHOOK_URL="http://${lan_ip}:5678" export N8N_PROTOCOL=http fi # CORS配置 export N8N_CORS_ENABLED=true if [ -n "$lan_ip" ]; then export N8N_CORS_ORIGINS="http://localhost:5678,http://${lan_ip}:5678,http://127.0.0.1:5678" fi print_success "环境变量已优化设置" echo " - 中文界面: 已启用" echo " - 权限检查: 已启用" echo " - 废弃警告: 已禁用" echo " - 网络监听: 0.0.0.0:5678" if [ -n "$lan_ip" ]; then echo " - 局域网IP: ${lan_ip}" fi } # 检查构建状态 check_build() { if [ ! -d "packages/cli/dist" ] || [ ! -d "packages/core/dist" ]; then print_warning "检测到项目需要构建" read -p "是否现在构建项目?(y/N): " build_project if [[ $build_project =~ ^[Yy]$ ]]; then print_info "正在构建项目..." pnpm build > build.log 2>&1 & BUILD_PID=$! while kill -0 $BUILD_PID 2>/dev/null; do echo -n "." sleep 2 done echo if wait $BUILD_PID; then print_success "项目构建完成" else print_error "构建失败,请查看 build.log" tail -20 build.log exit 1 fi fi fi } # 启动 n8n start_n8n() { print_info "正在启动优化版 n8n 中文版..." # 创建日志文件 local log_file="n8n-$(date +%Y%m%d-%H%M%S).log" # 启动 n8n(带优化参数) echo -e "${GREEN}正在启动 n8n 服务(无警告模式)...${NC}" NODE_NO_WARNINGS=1 NODE_OPTIONS="--no-deprecation" pnpm start > "$log_file" 2>&1 & N8N_PID=$! # 等待启动 echo -n "等待 n8n 启动" for i in {1..30}; do if curl -s http://localhost:5678 >/dev/null 2>&1; then break fi echo -n "." sleep 1 done echo # 检查是否启动成功 if curl -s http://localhost:5678 >/dev/null 2>&1; then print_success "n8n 启动成功!" # 获取局域网IP地址 local lan_ip="" if [[ "$OSTYPE" == "darwin"* ]]; then lan_ip=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | head -1 | awk '{print $2}') else lan_ip=$(hostname -I | awk '{print $1}') fi echo echo -e "${GREEN}╔════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ n8n 启动信息 ║${NC}" echo -e "${GREEN}╠════════════════════════════════════════╣${NC}" echo -e "${GREEN}║${NC} 📱 本地访问:" echo -e "${GREEN}║${NC} ${BLUE}http://localhost:5678${NC}" echo -e "${GREEN}║${NC}" if [ -n "$lan_ip" ]; then echo -e "${GREEN}║${NC} 🌐 局域网访问:" echo -e "${GREEN}║${NC} ${BLUE}http://${lan_ip}:5678${NC}" echo -e "${GREEN}║${NC}" echo -e "${GREEN}║${NC} 📌 提示: 统一使用以下地址" echo -e "${GREEN}║${NC} ${BLUE}http://${lan_ip}:5678${NC}" fi echo -e "${GREEN}╠════════════════════════════════════════╣${NC}" echo -e "${GREEN}║${NC} 🌏 界面语言: ${GREEN}中文 (zh-CN)${NC}" echo -e "${GREEN}║${NC} 📋 进程 PID: ${YELLOW}$N8N_PID${NC}" echo -e "${GREEN}║${NC} 📝 日志文件: ${YELLOW}$log_file${NC}" echo -e "${GREEN}║${NC} ✅ 无警告模式: ${GREEN}已启用${NC}" echo -e "${GREEN}╚════════════════════════════════════════╝${NC}" echo print_info "使用 Ctrl+C 停止服务" if [ -n "$lan_ip" ]; then echo print_success "优化项:" echo " ✓ 配置文件权限已修复" echo " ✓ Node.js 废弃警告已禁用" echo " ✓ 局域网访问已配置" echo " ✓ CORS 已正确设置" fi # 保持脚本运行并显示日志(过滤警告) trap 'print_info "正在停止 n8n..."; kill $N8N_PID 2>/dev/null; exit 0' INT tail -f "$log_file" | grep -v "DeprecationWarning" | grep -v "DEP0060" else print_error "n8n 启动失败" print_info "查看日志文件: $log_file" tail -20 "$log_file" exit 1 fi } # 检查并切换到正确目录 check_and_change_directory() { local current_dir="$(pwd)" local script_path="$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")" local script_dir="$(dirname "$script_path")" if [[ -f "package.json" && -d "packages" ]]; then print_info "检测到当前已在 n8n 项目目录" return 0 fi if [[ "$script_dir" == *"n8n-n8n-1.109.2"* ]]; then print_info "切换到脚本所在的 n8n 项目目录: $script_dir" cd "$script_dir" || { print_error "无法切换到目录: $script_dir" exit 1 } print_success "已切换到正确的 n8n 项目目录" return 0 fi print_error "未找到 n8n 项目目录" exit 1 } # 主函数 main() { # 显示横幅 show_banner # 检查并切换到正确目录 check_and_change_directory # 修复权限 fix_permissions # 检查依赖 check_dependencies # 检查端口 check_port # 停止现有进程 stop_existing # 设置环境变量 setup_environment # 检查构建状态 check_build # 正常启动 start_n8n } # 运行主函数 main "$@"