Nvm Keeps Forgetting Node Version in New Terminal Sessions: How to Fix
The Issue: Nvm relies on modifying your system's environment variable called PATH. This path tells the terminal where to find executable programs like node
and npm
. When you switch versions with nvm use
, it updates the PATH to point to the chosen version's binaries.
The problem arises because these changes are specific to the terminal session where you made them. When you close the terminal and open a new one, the PATH hasn't been updated with the nvm configuration. So, the new terminal doesn't know where to find the node.js version you set with nvm.
Possible Causes:
- Missing Initialization Script: Nvm works by adding initialization commands to your shell profile (like
.bashrc
or.zshrc
). These commands load nvm functionality whenever you open a terminal. If these commands are missing or not sourced correctly, nvm won't be active in new sessions. - Conflicting PATH modifications: Other programs or configurations might modify your PATH environment variable. This could overwrite nvm's changes, leading to the default system node.js version being used.
Solutions:
- Reinstall nvm: Sometimes a clean reinstall can fix configuration issues. You can uninstall nvm using Homebrew (
brew uninstall nvm
) and reinstall it (brew install nvm
). Follow the post-installation instructions to source the nvm script in your shell profile. - Verify Initialization Script: Check your shell profile (e.g.,
.bashrc
or.zshrc
) for the nvm initialization commands. These commands typically look likeexport NVM_DIR
andsource $(brew --prefix nvm)/nvm.sh
. Make sure they are present and not commented out.
export NVM_DIR=~/.nvm # Path to your nvm directory
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # Load nvm functionality
This code snippet sets the NVM_DIR
environment variable to your nvm directory and then sources the nvm.sh
script if it exists. This script adds nvm commands to your terminal session.
Checking Active Node Version:
nvm ls # List installed node versions
nvm current # Show the currently active version
These commands help you verify if nvm is working correctly. nvm ls
shows all installed versions, and nvm current
displays the version currently in use.
Switching Node Version:
nvm use <version_number> # Switch to a specific version (e.g., nvm use 16.17.0)
nvm alias default <version_number> # Set the default version for new sessions
These commands allow you to switch between installed versions and set a default version for future terminal sessions.
- A newer tool that manages not just Node.js but also other JavaScript tools like Yarn and npm.
- Offers similar functionality to nvm for installing and switching Node.js versions.
- More lightweight and faster compared to nvm.
asdf:
- A version manager for multiple languages, including Node.js, Python, Ruby, etc.
- Provides a unified way to manage different language versions.
- More complex setup compared to nvm or Volta.
n:
- A simpler Node.js version manager with a single command-line tool.
- Easier to install but offers fewer features compared to nvm or Volta.
- Installation: Use
npm install -g n
Node Version Switcher (nvs):
- Another alternative with a focus on simplicity.
Choosing the right alternative depends on your needs:
- Volta: Ideal if you manage other JavaScript tools alongside Node.js.
- asdf: Go for this if you need a single manager for multiple languages.
- n: Choose this for a simple and lightweight solution for just Node.js.
- nvs: Another option for a basic Node.js version manager.
node.js macos npm