NVM (Node Version Manager)
Installation
Prerequisites
# Install curl and build tools
sudo apt update
sudo apt install curl build-essential libssl-dev -yInstall NVM
# Download and install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Or using wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Reload shell configuration
source ~/.bashrc
# Or for zsh
source ~/.zshrc
# Verify installation
nvm --versionEnvironment Setup
Bash
# Add to ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Apply changes
source ~/.bashrcZsh
# Add to ~/.zshrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# Apply changes
source ~/.zshrcNode Version Management
List Available Versions
# List all available Node versions
nvm list-remote
# List remote versions with filter
nvm list-remote | grep "v16"
nvm list-remote --latest-10Install Node Versions
# Install specific version
nvm install 16.13.0
# Install latest LTS version
nvm install --lts
# Install latest version
nvm install node
# Install latest 16.x
nvm install 16List Installed Versions
# List locally installed versions
nvm list
nvm ls
# Show current version
nvm current
node --versionSwitch Between Versions
# Switch to specific version
nvm use 16.13.0
# Switch to LTS
nvm use --lts
# Switch to latest
nvm use node
# Switch to 16.x version
nvm use 16Set Default Version
# Set default version for new shells
nvm alias default 16.13.0
# View aliases
nvm aliasRemove Versions
# Uninstall specific version
nvm uninstall 16.13.0
# Uninstall all versions except current
nvm uninstall v14.19.0Project Configuration
.nvmrc File
# Create .nvmrc in project root
echo "16.13.0" > .nvmrc
# Or specify version
echo "v16.13.0" > .nvmrc
# Switch to .nvmrc version
nvm use
# Auto switch (requires shell hook)
# Add to ~/.bashrc or ~/.zshrc:
# [ -s ".nvm" ] && nvm useNPM Management
NPM Versions
# Check npm version
npm --version
# Update npm
npm install -g npm@latest
# Use specific npm version with Node
nvm install-latest-npmGlobal Packages
# Install global package
npm install -g package-name
# List global packages
npm list -g
# Update global packages
npm update -g
# Uninstall global package
npm uninstall -g package-nameTroubleshooting
# Check NVM installation
echo $NVM_DIR
# Reload NVM
source ~/.bashrc
# Check Node path
which node
which npm
# Verify npm registry
npm config get registry
# Reset npm cache
npm cache clean --force
# Check Node permissions
ls -la ~/.nvmAdvanced Usage
Virtual Environment per Project
# Create project directory
mkdir my-project
cd my-project
# Specify Node version
nvm use 16.13.0
# Install project dependencies
npm install
# Each developer uses same Node version from .nvmrcMultiple Node Versions for Testing
# Install multiple versions
nvm install 14.17.0
nvm install 16.13.0
nvm install 17.0.0
# Test with each version
nvm use 14.17.0 && npm test
nvm use 16.13.0 && npm test
nvm use 17.0.0 && npm testShell Integration
# Add NVM hook to automatically use version from .nvmrc
# Add to ~/.bashrc or ~/.zshrc
# Place this after NVM initialization
nvm_auto_use() {
if [ -f ".nvmrc" ]; then
nvm use
fi
}
# Call on directory change
cd() { builtin cd "$@"; nvm_auto_use; }Migration from Node.js
# If Node.js already installed
node --version
# Install same version with NVM
nvm install v14.17.0
# Switch to NVM version
nvm use 14.17.0
# Remove system Node.js (optional)
sudo apt remove nodejs -y
sudo apt remove npm -yUninstall NVM
# Remove NVM directory
rm -rf ~/.nvm
# Remove NVM lines from ~/.bashrc or ~/.zshrc
# Then reload shell
source ~/.bashrc