Skip to content

Instantly share code, notes, and snippets.

@papacasper
Created August 29, 2024 21:18
Show Gist options
  • Save papacasper/0d68d32599fe58d62ff6adcc3be80e47 to your computer and use it in GitHub Desktop.
Save papacasper/0d68d32599fe58d62ff6adcc3be80e47 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to print messages
print_message() {
echo "======================================="
echo "$1"
echo "======================================="
}
# Create the .nvm directory if it doesn't exist
if [ ! -d "$HOME/.nvm" ]; then
print_message "Creating .nvm directory..."
mkdir -p "$HOME/.nvm"
fi
# Install or update NVM (Node Version Manager)
print_message "Installing or updating NVM..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
# Source NVM script to make `nvm` command available in the script
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" || {
echo "Error: NVM was not installed correctly."
exit 1
}
# Install Node.js version 20
print_message "Installing Node.js version 20..."
nvm install 20
# Verify the Node.js installation
print_message "Verifying Node.js installation..."
NODE_VERSION=$(node -v)
EXPECTED_NODE_VERSION="v20.17.0"
if [ "$NODE_VERSION" == "$EXPECTED_NODE_VERSION" ]; then
echo "Node.js is correctly installed: $NODE_VERSION"
else
echo "Unexpected Node.js version: $NODE_VERSION (expected $EXPECTED_NODE_VERSION)"
fi
# Verify the npm installation
print_message "Verifying npm installation..."
NPM_VERSION=$(npm -v)
EXPECTED_NPM_VERSION="10.8.2"
if [ "$NPM_VERSION" == "$EXPECTED_NPM_VERSION" ]; then
echo "npm is correctly installed: $NPM_VERSION"
else
echo "Unexpected npm version: $NPM_VERSION (expected $EXPECTED_NPM_VERSION)"
fi
print_message "Installation and verification complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment