Created
June 19, 2025 20:16
-
-
Save StanleyMasinde/2c35ee256435c8624b6ecc5cc6e5e720 to your computer and use it in GitHub Desktop.
Update Node cleanly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
ARCH="linux-x64" | |
BASE_URL="https://nodejs.org/download/release" | |
log() { | |
echo -e "\033[1;32m==>\033[0m $1" | |
} | |
err() { | |
echo -e "\033[1;31m[ERROR]\033[0m $1" >&2 | |
} | |
log "Fetching latest current Node.js version..." | |
VERSION=$(wget -qO- "$BASE_URL/index.json" \ | |
| jq -r '[.[] | select(.lts == false)][0].version') || { err "Failed to fetch Node.js version"; exit 1; } | |
log "Installing Node.js $VERSION for $ARCH..." | |
TARBALL="node-${VERSION}-${ARCH}.tar.xz" | |
log "Downloading $TARBALL..." | |
wget -q "$BASE_URL/$VERSION/$TARBALL" || { err "Download failed"; exit 1; } | |
log "Extracting Node.js..." | |
tar -xf "$TARBALL" | |
## log "Stopping xx.service..." | |
## Here you stop any services that depend on Node.js | |
log "Installing to /usr/local..." | |
sudo cp -r "node-${VERSION}-${ARCH}"/{bin,include,lib,share} /usr/local/ | |
## log "Restarting xx.service..." | |
## Here reatart the services that depend on Node | |
log "Cleaning up..." | |
rm -rf "node-${VERSION}-${ARCH}" "$TARBALL" | |
log "Installed versions:" | |
echo -n " - node: " && node -v | |
echo -n " - npm: " && npm -v | |
log "Updating npm globally..." | |
sudo npm install -g npm || err "npm update failed" | |
log "✅ Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment