Skip to content

Instantly share code, notes, and snippets.

@romantech
Created June 16, 2024 10:31
Show Gist options
  • Save romantech/83ac87cea4e9a6d7a0985f610df329e5 to your computer and use it in GitHub Desktop.
Save romantech/83ac87cea4e9a6d7a0985f610df329e5 to your computer and use it in GitHub Desktop.
Update to Node.js LTS and Migrate Global NPM Packages
#!/bin/bash
# Define colors
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
BLUE=$(tput setaf 4)
YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)
# Function to handle errors
error_exit() {
echo "${RED}$1${RESET}" >&2
exit 1
}
# Save the currently used Node.js version
CURRENT_NODE_VERSION=$(fnm current) || error_exit "Failed to get current Node.js version"
echo "${GREEN}Current Node.js version: ${RESET}$CURRENT_NODE_VERSION"
# Save the list of currently installed global packages
GLOBAL_PACKAGES=$(npm list -g --depth=0 --json | jq -r '.dependencies | to_entries[] | "\(.key)@\(.value.version)"') || error_exit "Failed to list global packages"
echo "${GREEN}Current global packages:${RESET}"
echo "$GLOBAL_PACKAGES" | column -t
# Parse options
NODE_VERSION=""
while getopts "v:" opt; do
case $opt in
v) NODE_VERSION=$OPTARG ;;
\?) error_exit "Invalid option -$OPTARG" ;;
esac
done
# Determine the Node.js version to install
if [ -z "$NODE_VERSION" ]; then
echo "${BLUE}No version specified, installing Node.js LTS version${RESET}"
NODE_VERSION=$(fnm ls-remote --lts | tail -n 1 | awk '{print $1}') || error_exit "Failed to get LTS Node.js version"
else
echo "${BLUE}Installing Node.js version $NODE_VERSION${RESET}"
fi
# Check if the desired Node.js version is already installed
if fnm list | grep -q "$NODE_VERSION"; then
echo "${RED}Version $NODE_VERSION is already installed.${RESET}"
exit 0
fi
# Install the desired Node.js version
fnm install "$NODE_VERSION" || error_exit "Failed to install Node.js version $NODE_VERSION"
echo "${GREEN}Installed Node.js version: ${RESET}$NODE_VERSION"
# Switch to the desired Node.js version
fnm use "$NODE_VERSION" || error_exit "Failed to switch to Node.js version $NODE_VERSION"
# Install global packages
echo "${BLUE}Installing global packages${RESET}"
echo "$GLOBAL_PACKAGES" | while IFS=@ read -r package version; do
echo "${YELLOW}Installing package: ${RESET}$package@$version"
npm install -g "$package@$version" || error_exit "Failed to install $package@$version"
done
echo "${GREEN}Done! Global packages installed on Node.js $(fnm current) version.${RESET}"
@romantech
Copy link
Author

romantech commented Jun 16, 2024

Using fnm, this script installs the LTS or a specified version of Node.js, then reinstalls the global npm packages that were installed in the current Node.js version. It functions similarly to the --reinstall-packages-from option in nvm. If the -v flag is not specified, it will install the LTS version.

curl -fsSL https://gist.githubusercontent.com/romantech/83ac87cea4e9a6d7a0985f610df329e5/raw/decd27fb4fb3c0aa6563a74e000dc00974f6f1e9/update-node-lts.sh | bash -s -- -v <version>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment