nvm
nvm copied to clipboard
previously used node version is not removed from the PATH variable
Operating system and version:
macOS
nvm debug output:
nvm --version: v0.40.2
$TERM_PROGRAM: vscode
$SHELL: /bin/zsh
$SHLVL: 1
whoami: 'user'
${HOME}: /Users/user
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: ${NVM_DIR}/versions/node/v20.19.0/bin:${NVM_DIR}/versions/node/v22.14.0/bin:${NVM_DIR}/versions/node/v22.14.0/bin:${NVM_DIR}/versions/node/v20.19.0/bin:${HOME}/.pixi/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:${HOME}/.pixi/bin:${HOME}/Library/pnpm:${NVM_DIR}/versions/node/v18.20.6/bin:${HOME}/.cargo/bin:${HOME}/Library/Application Support/JetBrains/Toolbox/scripts:${HOME}/.spicetify:${HOME}/Library/Application Support/JetBrains/Toolbox/scripts:${HOME}/.spicetify
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.9 (arm64-apple-darwin24.0)'
uname -a: 'Darwin 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:24 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6030 arm64'
checksum binary: 'sha256sum'
OS version: macOS 15.3.2 24D81
awk: /usr/bin/awk, awk version 20200816
curl: /usr/bin/curl, curl 8.7.1 (x86_64-apple-darwin24.0) libcurl/8.7.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.63.0
wget: /opt/homebrew/bin/wget, GNU Wget 1.25.0 built on darwin24.1.0.
sed: /usr/bin/sed
cut: /usr/bin/cut
basename: /usr/bin/basename
rm: /bin/rm
mkdir: /bin/mkdir
xargs: /usr/bin/xargs
git: /usr/bin/git, git version 2.39.5 (Apple Git-154)
grep: /usr/bin/grep, grep (BSD grep, GNU compatible) 2.6.0-FreeBSD
nvm current: v20.19.0
which node: ${NVM_DIR}/versions/node/v20.19.0/bin/node
which iojs: iojs not found
which npm: ${NVM_DIR}/versions/node/v20.19.0/bin/npm
npm config get prefix: ${NVM_DIR}/versions/node/v20.19.0
npm root -g: ${NVM_DIR}/versions/node/v20.19.0/lib/node_modules
nvm ls output:
-> v20.19.0
v22.14.0
default -> 20 (-> v20.19.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v22.14.0) (default)
stable -> 22.14 (-> v22.14.0) (default)
lts/* -> lts/jod (-> v22.14.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.8 (-> N/A)
lts/iron -> v20.19.0
lts/jod -> v22.14.0
How did you install nvm?
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
What steps did you perform?
nvm install 20 nvm install 22 nvm use 20
What happened?
PATH variable is polluted with bin paths of the previously used node.
What did you expect to happen?
PATH to only contain selected node
Is there anything in any of your profile files that modifies the PATH?
no
Interesting - there's explicit code in nvm to purge the PATH of nvm entries before using a new version. What happens if you manually clear out the nvm stuff from the PATH, and then nvm use 20?
then it's added once. I also noticed that it only happens in vscode terminal, but not in the system terminal
oh interesting. so you can't reproduce the problem in a real terminal, but you can consistently reproduce it in vscode's terminal?
exactly!
nvm alias default v22.15.0
nvm use default
After restarting the terminal: v16.20.2
@xianghongai same problem for you? works on the real terminal, not on vscode's?
Hi, I'm new to programming and open source development world, so I don't know if this is actually useful, but anyway, I ran the following test:
- Open system terminal
- Open VSCode and start a terminal session
- Change the
default aliasin the system terminal to another version nvm default aliasis successfully updated in VSCode terminal ✅- Open a new system terminal window
- The new system terminal uses the new defined
default alias✅ - Open a new terminal session in VSCode
- The new terminal session in VSCode uses the old
default alias❌
It seems like VSCode retains the inicial PATH for new terminal sessions. That inicial PATH persists during VSCode running, and only updates when I close and reopen it.
I ran the test changing steps 3 and 4 (marked with a ⭐). Instead of using the system terminal, I used the VSCode terminal:
- Open system terminal
- Open VSCode and start a terminal session
- Change the
default aliasin the VSCode terminal to another version ⭐ nvm default aliasis successfully updated in system terminal ✅ ⭐- Open a new system terminal window
- The new system terminal uses the new defined
default alias✅ - Open a new terminal session in VSCode
- The new terminal session in VSCode uses the old
default alias❌
Sounds like it's vscode-specific behavior, then, that nvm wouldn't be able to fix.
I was wrong. I ran a simple test in VSCode terminal by modifying the PATH in ~/.zshrc. It effectively reloaded the PATH when I opened a new terminal session. Steps:
- Open system terminal
- Open VSCode and start a terminal session
- Change the
.zshrcadding some thing to the PATH - Open a new terminal session in VSCode
- The new terminal session in VSCode uses the modified PATH ✅
So, if that isn't a VSCode issue, maybe it's reproducible in another similar environment. Then, I ran the same 8 steps of my previous comment, replacing VSCode with Tmux. And yes - the same issue: I also need to create a completely new session to have it initialized with the new alias default.
nvm alias default v22.15.0nvm use defaultAfter restarting the terminal: v16.20.2
I have the same problem
Just chiming in, I can also reproduce this issue on a real terminal, not VSCode (Ghosstty terminal to be exact).
Steps:
- New terminal (node v16.20.2)
nvm use default(set to lts/* - 22.14.0)node --version= 22.14.0nvm debug
${PATH}: ${NVM_DIR}/versions/node/v22.14.0/bin:${HOME}/.config/bin:${HOME}/.pyenv/shims:${NVM_DIR}/versions/node/v16.20.2/bin:${HOME}/.config/herd-lite/bin:/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:${HOME}/bin:${HOME}/.config/phpmon/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:${HOME}/.config/bin:${NVM_DIR}/versions/node/v16.20.2/bin:${HOME}/.config/herd-lite/bin:/Applications/iTerm.app/Contents/Resources/utilities:${HOME}/.composer/vendor/bin:${HOME}/Library/Application Support/JetBrains/Toolbox/scripts:/Applications/Ghostty.app/Contents/MacOS
Note the path still contains reference to v16.20.2 node, which is what was set before using nvm use default.
Also as @xianghongai described, new terminals aren't respecting the default alias that I have set. All terminals start with v16.20.2 by default. Once nvm use default is ran, node --version correctly returns v22, but only in that terminal session.
and new a terminal run node -v
So I failed to switch the node version using nvm, only the current terminal switched successfully