nvm keeps forgetting node in new terminal session
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When you install Node.js using Node Version Manager (nvm), you might encounter an issue where nvm does not remember the version of Node you've selected across new terminal sessions. This can be frustrating, particularly when using tools or scripts that rely on a specific Node version. Understanding why this happens and how to fix it is crucial for maintaining a smooth development environment.
Understanding the Problem
Node Version Manager (nvm) is a version manager for Node.js, designed to help manage and switch between different versions of Node.js and npm. It does this by adjusting environment variables and paths to point to specific Node.js versions installed in your home directory under ~/.nvm/versions/node.
The issue arises primarily due to how nvm manages these versions and environment settings. When you open a new terminal session, the session starts with the default system environment settings until it reads the configuration files like .bashrc, .bash_profile, .zshrc, etc., depending on your shell.
Common Reasons for the Issue
- Non-persistent Setting: nvm uses a shell script to modify PATH variables dynamically. If nvm settings are not sourced in the shell’s startup script (like
.bashrcor.zshrc), the modification will not persist across terminal sessions. - Incorrect Shell Configuration File: Users often place nvm initialization scripts in the wrong configuration file for their terminal or they are using a shell that doesn't read the file where they added the initialization script.
- Conflict with System Node Installation: Sometimes, if Node.js is also installed through another method like a package manager (apt, brew), it may conflict with the version managed by nvm.
Fixing the Problem
To ensure that nvm settings persist across terminal sessions, you need to add the nvm initialization script to your shell's startup file. Here’s how you can do it for some common shells:
Bash
If you are using Bash, you should add the following line to your .bashrc or .bash_profile:
Zsh
For Zsh, include the line in your .zshrc:
Verification
To verify that nvm will now correctly remember your Node version across new terminal tabs and windows, try the following:
- Open a terminal and install a version of Node if haven’t yet, e.g.,
nvm install 14. - Use a specific version:
nvm use 14. - Open a new terminal tab/window and type
node -v. It should displayv14.x.x.
Summary Table
| Issue Component | Problem | Solution |
| Shell Startup Configuration | Missing nvm initialization in shell startup script. | Add nvm source line to .bashrc or .zshrc. |
| Multiple Node.js Installations | System's node installation conflicts with nvm. | Uninstall system Node.js or adjust PATH. |
| Wrong Configuration File | Edit wrong configuration file (e.g., .bashrc for zsh user). | Add configuration to correct shell file. |
Additional Tips
- Automatically Switch Node Versions: Use the avn tool, which automatically switches Node versions per directory based on a
.nvmrcfile. - Troubleshooting: If the changes do not take effect, restart your terminal or use
source ~/.bashrc(or corresponding shell configuration file) to reload the configuration. - nvm Aliases: Use aliases (
nvm alias default node) to set a default Node version for new shells.
By understanding how nvm interacts with your shell environment and configuring it correctly, you can ensure a seamless development process without having to manually switch node versions for every new terminal session.
Related reading
- Object comparison in JavaScript
- Object is enumerable but not indexable?
- offsetting an html anchor to adjust for fixed header
- OpenAI GPT-2 model use with TensorFlow JS
- object kafka is not a member of package org.apache
- Object of type 'ndarray' is not JSON serializable
- OpenAI GPT-2 model use with TensorFlow JS
- optimal algorithm grouping data in javascript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.