BASH
Manage Node.js Versions with NVM
Learn how to efficiently switch between different Node.js versions using nvm (Node Version Manager) in your development environment for various project requirements.
#!/bin/bash
# Install a specific Node.js version
# nvm install 18
# nvm install 16
# Use a specific Node.js version for the current shell session
# nvm use 18
# Set a default Node.js version
# nvm alias default 18
# List all installed Node.js versions
nvm ls
# Install the latest LTS version and set it as default
echo "Installing latest LTS Node.js and setting as default..."
nvm install --lts
nvm alias default $(nvm ls --lts | grep -o 'v[0-9]*\.[0-9]*\.[0-9]*' | head -n 1)
echo "Current Node.js version:"
node -v
echo "To install nvm, visit: https://github.com/nvm-sh/nvm"
How it works: This snippet demonstrates common `nvm` commands for managing Node.js versions. It shows how to list installed versions, install the latest Long Term Support (LTS) version, and set it as the default for new shell sessions. `nvm` is crucial for web developers working on multiple projects requiring different Node.js environments, ensuring compatibility and preventing conflicts.