#!/bin/bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
NODEJS_VERSION="22"
NVM_INSTALL_URL="https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh"
check_node_installed() {
if command -v node &> /dev/null; then
INSTALLED_VERSION=$(node -v | cut -d 'v' -f 2 | cut -d '.' -f 1)
if [ "$INSTALLED_VERSION" = "$NODEJS_VERSION" ]; then
return 0
else
echo -e "${YELLOW}Node.js is installed but version is $(node -v), not the specified v${NODEJS_VERSION}${NC}"
return 1
fi
else
return 1
fi
}
check_and_install_homebrew() {
if ! command -v brew &> /dev/null; then
echo -e "${YELLOW}Homebrew not installed, starting Homebrew installation...${NC}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if [ -f "/opt/homebrew/bin/brew" ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -f "/usr/local/bin/brew" ]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
fi
}
install_nvm() {
echo -e "${YELLOW}\nNVM not detected, installing NVM first...${NC}"
if ! command -v curl &> /dev/null; then
echo -e "${YELLOW}NVM installation depends on curl, installing curl first...${NC}"
check_and_install_homebrew
brew install curl
fi
curl -o- "$NVM_INSTALL_URL" | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
if [ -n "${ZSH_VERSION:-}" ]; then
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi
if command -v nvm &> /dev/null; then
echo -e "${GREEN}✅ NVM installation successful! Version: $(nvm --version)${NC}"
else
echo -e "${RED}❌ NVM installation failed, please check manually!${NC}"
exit 1
fi
}
install_nodejs() {
echo -e "${YELLOW}\nStarting Node.js v${NODEJS_VERSION} installation...${NC}"
if ! command -v nvm &> /dev/null; then
install_nvm
fi
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
if [ -n "${ZSH_VERSION:-}" ]; then
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi
nvm install "$NODEJS_VERSION"
nvm alias default "$NODEJS_VERSION"
nvm use default > /dev/null 2>&1
}
verify_nodejs() {
if check_node_installed; then
NODE_VERSION=$(node -v)
NPM_VERSION=$(npm -v)
echo -e "${GREEN}✅ Node.js installation successful! Version: ${NODE_VERSION}${NC}"
echo -e "${GREEN}✅ NPM installation successful! Version: ${NPM_VERSION}${NC}"
echo -e "${YELLOW}Testing Node.js functionality...${NC}"
if node -e "console.log('Node.js functionality normal')" &> /dev/null; then
echo -e "${GREEN}✅ Node.js functionality test passed!${NC}"
else
echo -e "${YELLOW}⚠️ Node.js is installed but functionality test abnormal${NC}"
fi
else
echo -e "${RED}❌ Node.js installation failed, please check manually!${NC}"
exit 1
fi
}
echo -e "${YELLOW}=== Starting Node.js v${NODEJS_VERSION} installation status check ===${NC}"
if check_node_installed; then
NODE_VERSION=$(node -v)
echo -e "${GREEN}Node.js v${NODEJS_VERSION} is installed, current version: ${NODE_VERSION}${NC}"
verify_nodejs
else
echo -e "${RED}Node.js v${NODEJS_VERSION} is not installed${NC}"
echo -e "${YELLOW}Checking network connection...${NC}"
if ! curl -s -o /dev/null -w "%{http_code}" --max-time 5 https://www.baidu.com > /dev/null 2>&1; then
echo -e "${RED}Error: Basic network connection failed, please check network settings${NC}"
exit 1
fi
GITHUB_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 https://github.com 2>/dev/null || echo "000")
if [[ ! "$GITHUB_STATUS" =~ ^[23][0-9]{2}$ ]]; then
echo -e "${YELLOW}⚠️ Warning: Cannot directly access GitHub (status code: ${GITHUB_STATUS})${NC}"
echo -e "${YELLOW}Attempting to check NVM installation source...${NC}"
NVM_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$NVM_INSTALL_URL" 2>/dev/null || echo "000")
if [[ ! "$NVM_STATUS" =~ ^[23][0-9]{2}$ ]]; then
echo -e "${RED}Error: Cannot access NVM installation source (status code: ${NVM_STATUS})${NC}"
echo -e "${YELLOW}Tip: If in mainland China, may need to configure proxy or use mirror source${NC}"
echo -e "${YELLOW}Can try setting proxy: export https_proxy=http://your-proxy:port${NC}"
exit 1
else
echo -e "${GREEN}✅ NVM installation source accessible${NC}"
fi
else
echo -e "${GREEN}✅ GitHub access normal (status code: ${GITHUB_STATUS})${NC}"
fi
install_nodejs
verify_nodejs
fi
echo -e "\n${GREEN}=== Operation completed ===${NC}"
echo -e "${YELLOW}Tip: If node command not available in new terminal, execute source ~/.zshrc or source ~/.bash_profile or restart terminal${NC}"
exit 0