nvm icon indicating copy to clipboard operation
nvm copied to clipboard

previously used node version is not removed from the PATH variable

Open 0xorial opened this issue 7 months ago • 12 comments
trafficstars

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

0xorial avatar Apr 08 '25 15:04 0xorial

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?

ljharb avatar Apr 08 '25 16:04 ljharb

then it's added once. I also noticed that it only happens in vscode terminal, but not in the system terminal

0xorial avatar Apr 08 '25 17:04 0xorial

oh interesting. so you can't reproduce the problem in a real terminal, but you can consistently reproduce it in vscode's terminal?

ljharb avatar Apr 08 '25 17:04 ljharb

exactly!

0xorial avatar Apr 08 '25 18:04 0xorial

nvm alias default v22.15.0 nvm use default

After restarting the terminal: v16.20.2

xianghongai avatar Apr 28 '25 07:04 xianghongai

@xianghongai same problem for you? works on the real terminal, not on vscode's?

ljharb avatar Apr 28 '25 17:04 ljharb

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:

  1. Open system terminal
  2. Open VSCode and start a terminal session
  3. Change the default alias in the system terminal to another version
  4. nvm default alias is successfully updated in VSCode terminal ✅
  5. Open a new system terminal window
  6. The new system terminal uses the new defined default alias
  7. Open a new terminal session in VSCode
  8. 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:

  1. Open system terminal
  2. Open VSCode and start a terminal session
  3. Change the default alias in the VSCode terminal to another version ⭐
  4. nvm default alias is successfully updated in system terminal ✅ ⭐
  5. Open a new system terminal window
  6. The new system terminal uses the new defined default alias
  7. Open a new terminal session in VSCode
  8. The new terminal session in VSCode uses the old default alias

ghost avatar Apr 28 '25 23:04 ghost

Sounds like it's vscode-specific behavior, then, that nvm wouldn't be able to fix.

ljharb avatar Apr 28 '25 23:04 ljharb

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:

  1. Open system terminal
  2. Open VSCode and start a terminal session
  3. Change the .zshrc adding some thing to the PATH
  4. Open a new terminal session in VSCode
  5. 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.

ghost avatar Apr 29 '25 03:04 ghost

nvm alias default v22.15.0 nvm use default

After restarting the terminal: v16.20.2

I have the same problem

atox996 avatar Apr 30 '25 04:04 atox996

Just chiming in, I can also reproduce this issue on a real terminal, not VSCode (Ghosstty terminal to be exact).

Steps:

  1. New terminal (node v16.20.2)
  2. nvm use default (set to lts/* - 22.14.0)
  3. node --version = 22.14.0
  4. nvm 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.

mattyforth avatar May 12 '25 09:05 mattyforth

Image

and new a terminal run node -v

Image

So I failed to switch the node version using nvm, only the current terminal switched successfully

SQDR2 avatar Jun 09 '25 09:06 SQDR2