.bashrc and .profile (or .bash_profile) contain source commands for the nvm script. This duplication causes the shell to load the NVM environment multiple times during session startup, leading to confusion and potential conflicts in variable definitions. Resolving this requires auditing your dotfiles to ensure NVM is initialized exactly once per shell type.
Understanding Shell Initialization Order
To fix duplicate NVM entries, you must first understand how Bash loads configuration files. When you open a terminal, it usually starts as an interactive login shell or a non-login interactive shell. The behavior differs slightly between Proxmox VE host systems and standard Debian installations, but the core logic remains consistent. For an interactive login shell (such as when you SSH in or start a new console session), Bash reads~/.bash_profile, ~/.bash_login, and ~/.profile in that order, executing only the first one it finds. It does not read ~/.bashrc automatically unless explicitly sourced from one of those files.
For an interactive non-login shell (such as opening a new tab in a GUI terminal emulator), Bash reads ~/.bashrc. If you have placed the NVM initialization code in both ~/.bashrc and ~/.profile, you might see it load twice depending on how your shell transitions between states or if you manually source files.
Identifying the Conflict
The most common scenario involves users copying NVM installation instructions that suggest adding lines to~/.bashrc. However, many Linux distributions also link ~/.profile to ~/.bashrc or vice versa, or users manually add sourcing commands to both. Check your home directory for these files:
~/.bashrc~/.profile~/.bash_profile
nvm. You are looking for lines similar to export NVM_DIR="$HOME/.nvm" followed by [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh". If this block appears in more than one file, or if one file sources another which contains the same block, you have found the root cause.
Step-by-Step Resolution
Follow these steps to consolidate your NVM initialization into a single location. We recommend keeping the initialization in~/.bashrc because it applies to all interactive shells, ensuring NVM is available regardless of how you log in.
- Backup your configuration files: Before making changes, create backups to prevent data loss.
cp ~/.bashrc ~/.bashrc.backup cp ~/.profile ~/.profile.backup - Edit
~/.bashrc: Open the file in your preferred text editor.
Ensure the NVM initialization block is present at the end of the file. It should look like this:nano ~/.bashrc
If it is already there, proceed to the next step. If not, append it.export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion - Remove duplicates from other files: Open
~/.profileand~/.bash_profile.
If you find the NVM initialization block here, remove it. Yournano ~/.profile~/.profileshould ideally source~/.bashrcto ensure consistency, so check if it contains:
This ensures that login shells also pick up the settings from# if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi.bashrc, including NVM. - Verify no other scripts interfere: Some Proxmox configurations or custom scripts might source files dynamically. Check
/etc/profileand/etc/bash.bashrcfor any global NVM references, though this is rare for user-specific tools like NVM.
source ~/.bashrc. To verify the fix, run nvm --version. If it returns a version number without errors or duplicate messages, the issue is resolved.
Proxmox Specific Considerations
When managing Debian VMs within Proxmox, remember that the host system and the guest OS have separate environments. The NVM issue occurs entirely within the Debian guest. If you are accessing the VM via the Proxmox web interface's console, it acts as a direct TTY, often triggering login shell behavior. If you SSH into the VM, it may behave differently depending on your SSH client configuration. Ensure that your SSH key authentication and network settings are correct. For related issues with connectivity or access, such as resetting credentials or troubleshooting SSH handshake errors, refer to our guide on How to Reset Root Password for a VM Running on Proxmox and Troubleshooting kex_exchange_identification Error in Linux SSH Connection. These resources can help if you suspect broader configuration issues alongside the NVM duplication.Common Pitfalls
Even after fixing the initialization files, some users encounter residual issues. Ensure you are not using aliases that redefinenvm or npm. Check your ~/.aliases file if you use one. Additionally, avoid installing Node.js via apt while using NVM, as they can conflict. NVM manages its own Node versions in ~/.nvm/versions/node. Using apt installed Node can override the path set by NVM, causing confusion about which version is active.
Always verify your current Node version with node -v and your NVM status with nvm ls. If nvm ls shows multiple "default" arrows or strange paths, clean up your ~/.nvm/alias/default file to point to a single version.
Frequently Asked Questions
Why does NVM still show twice after editing .bashrc?
If you see duplicate outputs, check if you are sourcing .bashrc multiple times within the file itself or if another script in your PATH is calling it. Use echo $NVM_DIR to verify if the variable is being redefined. Clearing your shell history and starting a fresh terminal instance can also help rule out cached state issues.
Can I use NVM with Zsh instead of Bash?
Yes, but you must configure it for Zsh specifically. Instead of modifying .bashrc, you need to add the NVM initialization lines to ~/.zshrc. The syntax is identical, but the file location differs. Ensure you do not mix Bash and Zsh configuration files, as they are not compatible.
Does NVM affect the Proxmox host system?
No, NVM installed inside a Debian VM only affects that specific virtual machine. The Proxmox host runs its own Debian-based OS and has separate environment variables. Changes made to .bashrc inside the VM will never impact the host's shell configuration or installed packages.
How do I completely uninstall NVM to start over?
To remove NVM, delete the ~/.nvm directory, remove the initialization lines from ~/.bashrc and ~/.profile, and delete any associated symlinks in /usr/local/bin created by npm. Then restart your shell. This provides a clean slate for reinstallation.
What if I need different Node versions for different projects?
NVM allows you to set local versions per directory using nvm use or by creating a .nvmrc file in your project root. This feature works independently of the shell initialization fix. Once NVM is loaded correctly once, you can switch versions seamlessly without worrying about duplicate definitions.