nvm
node.js
terminal session
troubleshooting
programming

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.

Browse interview questions

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

  1. 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 .bashrc or .zshrc), the modification will not persist across terminal sessions.
  2. 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.
  3. 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:

bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Zsh

For Zsh, include the line in your .zshrc:

zsh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Verification

To verify that nvm will now correctly remember your Node version across new terminal tabs and windows, try the following:

  1. Open a terminal and install a version of Node if haven’t yet, e.g., nvm install 14.
  2. Use a specific version: nvm use 14.
  3. Open a new terminal tab/window and type node -v. It should display v14.x.x.

Summary Table

Issue ComponentProblemSolution
Shell Startup ConfigurationMissing nvm initialization in shell startup script.Add nvm source line to .bashrc or .zshrc.
Multiple Node.js InstallationsSystem's node installation conflicts with nvm.Uninstall system Node.js or adjust PATH.
Wrong Configuration FileEdit 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 .nvmrc file.
  • 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.