NVM Node Version Not Switching Fix
In this tutorial, you'll learn about NVM Node Version Not Switching Fix. We cover key concepts, practical examples, and best practices.
The Problem
You run nvm use 18 but node --version still shows the old version. Or you open a new terminal and the Node version reverts. NVM changes are shell-specific and do not persist across sessions.
Quick Fix
Step 1: Run nvm use in the current shell
# Wrong — nvm not activated for this shell
node --version
# v16.0.0
# Right — switch version in this shell
nvm use 18
node --version
# v18.17.0
Expected output: The Node version changes for the current terminal session.
Step 2: Set a default Node version
nvm alias default 18
Expected output: New shells default to Node 18.
Step 3: Source nvm in your shell profile
# Add to ~/.zshrc or ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Expected output: NVM is available in every new terminal.
Step 4: Use .nvmrc for project-specific versions
# In project root
echo "18" > .nvmrc
# Then in the project directory:
nvm use
# Automatically reads .nvmrc and switches to Node 18
Expected output: Entering the project directory switches to the correct Node version.
Step 5: Automatically switch with shell hook
Add to ~/.zshrc:
autoload -U add-zsh-hook
load-nvmrc() {
if [[ -f .nvmrc && -r .nvmrc ]]; then
nvm use
fi
}
add-zsh-hook chpwd load-nvmrc
Expected output: The Node version switches automatically when cding into a project with .nvmrc.
Step 6: Check installed versions
nvm ls
Expected output: Lists all installed Node versions with aliases.
-> v18.17.0
v20.11.0
default -> 18 (-> v18.17.0)
Step 7: Install a missing version
nvm install 20
nvm use 20
Expected output: Node 20 is installed and activated.
Prevention
- Set a default alias:
nvm alias default 18 - Create
.nvmrcin every project - Add NVM sourcing to your shell profile
- Use
nvm lsto confirm versions are installed before switching
Common Mistakes with node version
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world NVM code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro