-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.sh
More file actions
66 lines (54 loc) · 1.76 KB
/
node.sh
File metadata and controls
66 lines (54 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
set -euo pipefail
# ---------- helpers ----------
log() { echo -e "\n==> $*"; }
need_cmd() { command -v "$1" >/dev/null 2>&1; }
if [[ "${EUID}" -ne 0 ]]; then
echo "Please run as root: sudo $0"
exit 1
fi
# Default Node.js version
DEFAULT_VERSION=24
# ---------- get version ----------
NODE_MAJOR="${1:-}"
if [[ -z "${NODE_MAJOR}" ]]; then
echo ""
echo "Available LTS versions: 18, 20, 22, 24"
read -p "Enter Node.js major version [${DEFAULT_VERSION}]: " NODE_MAJOR
NODE_MAJOR="${NODE_MAJOR:-${DEFAULT_VERSION}}"
fi
# Validate version is a number
if ! [[ "${NODE_MAJOR}" =~ ^[0-9]+$ ]]; then
echo "ERROR: Version must be a number (e.g., 18, 20, 22, 24)"
exit 1
fi
log "Installing Node.js version: ${NODE_MAJOR}.x"
# ---------- check if already installed ----------
if need_cmd node; then
CURRENT_VERSION=$(node -v | sed 's/v//' | cut -d. -f1)
if [[ "${CURRENT_VERSION}" -eq "${NODE_MAJOR}" ]]; then
log "Node.js ${NODE_MAJOR} already installed: $(node -v)"
log "npm: $(npm -v)"
exit 0
fi
log "Node.js found: $(node -v), will install v${NODE_MAJOR}"
fi
# ---------- install prerequisites ----------
log "Installing prerequisites..."
apt-get update -y
apt-get install -y --no-install-recommends ca-certificates curl gnupg
# ---------- add nodesource repository ----------
log "Adding NodeSource repository for Node.js ${NODE_MAJOR}..."
curl -fsSL https://deb.nodesource.com/setup_${NODE_MAJOR}.x | bash -
# ---------- install node ----------
log "Installing Node.js ${NODE_MAJOR}..."
apt-get install -y nodejs
log "Done."
echo ""
echo "=========================================="
echo " Node.js Installation Complete!"
echo "=========================================="
echo ""
echo "Node.js: $(node -v)"
echo "npm: $(npm -v)"
echo ""