flnxi-Wu/scripts/tools/check-nodejs-version.sh
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

158 lines
4.2 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Node.js 版本诊断脚本
echo "=========================================="
echo " Node.js 环境诊断工具"
echo "=========================================="
echo ""
echo "[1] 当前 Node.js 版本"
echo "-----------------------------------"
if command -v node &> /dev/null; then
NODE_VERSION=$(node -v)
echo "✅ Node.js: $NODE_VERSION"
MAJOR_VERSION=$(echo $NODE_VERSION | cut -d'.' -f1 | sed 's/v//')
if [ "$MAJOR_VERSION" -ge 18 ]; then
echo "✅ 版本满足要求 (需要 ≥18.x)"
else
echo "❌ 版本不满足要求 (当前: v$MAJOR_VERSION, 需要: ≥18)"
fi
else
echo "❌ Node.js 未安装"
fi
echo ""
echo "[2] npm 版本"
echo "-----------------------------------"
if command -v npm &> /dev/null; then
NPM_VERSION=$(npm -v)
echo "✅ npm: $NPM_VERSION"
else
echo "❌ npm 未安装"
fi
echo ""
echo "[3] Node.js 安装路径"
echo "-----------------------------------"
which node
which npm
echo ""
echo "[4] 宝塔面板 Node.js 版本"
echo "-----------------------------------"
if [ -d "/www/server/nodejs" ]; then
echo "已安装的版本:"
ls -1 /www/server/nodejs/ | grep -E "^v[0-9]" | while read version; do
NODE_PATH="/www/server/nodejs/$version/bin/node"
if [ -f "$NODE_PATH" ]; then
VERSION=$($NODE_PATH -v 2>/dev/null)
MAJOR=$(echo $VERSION | cut -d'.' -f1 | sed 's/v//')
if [ "$MAJOR" -ge 18 ]; then
echo " ✅ $version ($VERSION) - 可用"
else
echo " ⚠️ $version ($VERSION) - 版本太低"
fi
fi
done
else
echo "⚠️ 未检测到宝塔面板 Node.js 安装目录"
fi
echo ""
echo "[5] nvm 状态"
echo "-----------------------------------"
if command -v nvm &> /dev/null; then
echo "✅ nvm 已安装"
nvm list
elif [ -d "$HOME/.nvm" ]; then
echo "✅ nvm 已安装(需要加载)"
echo "执行: source ~/.nvm/nvm.sh"
else
echo "⚠️ nvm 未安装"
fi
echo ""
echo "[6] 项目依赖检查"
echo "-----------------------------------"
if [ -f "package.json" ]; then
echo "✅ package.json 存在"
# 检查 engines 字段
if grep -q "engines" package.json; then
echo "项目要求的 Node 版本:"
grep -A 2 "engines" package.json
fi
# 检查主要依赖
echo ""
echo "关键依赖版本:"
grep -E "(next|prisma|@prisma/client)" package.json | sed 's/^/ /'
else
echo "❌ package.json 不存在"
fi
echo ""
echo "[7] Prisma 状态"
echo "-----------------------------------"
if [ -d "node_modules/@prisma/client" ]; then
echo "✅ Prisma 客户端已安装"
if [ -f "node_modules/.prisma/client/index.js" ]; then
echo "✅ Prisma 客户端已生成"
else
echo "⚠️ Prisma 客户端未生成,需要运行: npx prisma generate"
fi
else
echo "❌ Prisma 客户端未安装"
fi
echo ""
echo "[8] 环境变量"
echo "-----------------------------------"
echo "PATH:"
echo "$PATH" | tr ':' '\n' | grep -E "(node|npm)" | sed 's/^/ /'
echo ""
echo "=========================================="
echo " 诊断完成"
echo "=========================================="
echo ""
# 给出建议
NODE_MAJOR=0
if command -v node &> /dev/null; then
NODE_MAJOR=$(node -v | cut -d'.' -f1 | sed 's/v//')
fi
if [ "$NODE_MAJOR" -lt 18 ]; then
echo "🎯 推荐操作:"
echo ""
echo "1️⃣ 使用宝塔面板安装 Node.js 18:"
echo " 软件商店 → Node版本管理器 → 安装 v18.x"
echo ""
echo "2️⃣ 或使用命令行安装:"
if [ -d "/www/server/nodejs/v18.20.0" ]; then
echo " export PATH=/www/server/nodejs/v18.20.0/bin:\$PATH"
else
echo " curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -"
echo " dnf install -y nodejs"
fi
echo ""
echo "3️⃣ 然后重新安装依赖:"
echo " cd /www/wwwroot/sucai"
echo " rm -rf node_modules package-lock.json"
echo " npm install"
echo ""
else
echo "✅ Node.js 版本正常"
echo ""
echo "🎯 如果还有问题,尝试:"
echo " rm -rf node_modules package-lock.json"
echo " npm cache clean --force"
echo " npm install"
echo ""
fi