nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
MIT License
80.38k stars 8.03k forks source link

Minor issue: `nvm current` returns blank/nothing on Windows w/ Git Bash environment #3162

Open jschillingApollo opened 1 year ago

jschillingApollo commented 1 year ago

Operating system and version:

Windows 10, nvm version 0.39.1

nvm debug output:

```sh $ nvm debug nvm --version: v0.39.1 $SHELL: /usr/bin/bash $SHLVL: 1 whoami: 'jschilling' ${HOME}: /c/Users/jschilling ${NVM_DIR}: '${HOME}/.nvm' ${PATH}: ${HOME}/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:${HOME}/bin:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files/Azure Data Studio/bin:/c/Program Files/dotnet:/c/Windows/system32/config/systemprofile/.dnx/bin:/c/Program Files/Microsoft DNX/Dnvm:/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/c/Program Files/Microsoft/Web Platform Installer:/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/cmd:${HOME}/AppData/Local/Programs/Microsoft VS Code/bin:${HOME}/AppData/Local/.meteor:${NVM_DIR}/versions/node/v12.19.1/bin/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:${NVM_DIR}/versions/node/v12.19.1/bin/node_modules/meteor/node_modules/.bin:${NVM_DIR}/versions/node/v12.19.1/bin/node_modules/.bin:${NVM_DIR}/versions/node/v12.19.1/bin:${HOME}/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/usr/bin:/mingw64/bin:/usr/bin:${HOME}/bin:${HOME}/.dnx/bin:/c/Program Files/Microsoft SQL Server/130/Tools:/usr/bin/vendor_perl:/usr/bin/core_perl $PREFIX: '' ${NPM_CONFIG_PREFIX}: '' $NVM_NODEJS_ORG_MIRROR: '' $NVM_IOJS_ORG_MIRROR: '' shell version: 'GNU bash, version 5.2.15(1)-release (x86_64-pc-msys)' uname -a: 'MINGW64_NT-10.0-19045 3.4.7-ea781829.x86_64 2023-07-05 12:05 UTC x86_64 Msys' checksum binary: 'sha256sum' curl: /mingw64/bin/curl, curl 8.1.2 (x86_64-w64-mingw32) libcurl/8.1.2 Schannel zlib/1.2.13 brotli/1.0.9 zstd/1.5.5 libidn2/2.3.4 libpsl/0.21.2 (+libidn2/2.3.3) libssh2/1.11.0 wget: not found git: /mingw64/bin/git, git version 2.41.0.windows.3 grep: /usr/bin/grep, grep (GNU grep) 3.0 awk: /usr/bin/awk, GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.2.0-p9, GNU MP 6.2.1) sed: /usr/bin/sed, sed (GNU sed) 4.9 cut: /usr/bin/cut, cut (GNU coreutils) 8.32 basename: /usr/bin/basename, basename (GNU coreutils) 8.32 rm: /usr/bin/rm, rm (GNU coreutils) 8.32 mkdir: /usr/bin/mkdir, mkdir (GNU coreutils) 8.32 xargs: /usr/bin/xargs, xargs (GNU findutils) 4.9.0 nvm current: which node: ${NVM_DIR}/versions/node/v12.19.1/bin/node which iojs: which: no iojs in (${HOME}/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:${HOME}/bin:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files/Azure Data Studio/bin:/c/Program Files/dotnet:/c/Windows/system32/config/systemprofile/.dnx/bin:/c/Program Files/Microsoft DNX/Dnvm:/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/c/Program Files/Microsoft/Web Platform Installer:/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/cmd:${HOME}/AppData/Local/Programs/Microsoft VS Code/bin:${HOME}/AppData/Local/.meteor:${NVM_DIR}/versions/node/v12.19.1/bin/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:${NVM_DIR}/versions/node/v12.19.1/bin/node_modules/meteor/node_modules/.bin:${NVM_DIR}/versions/node/v12.19.1/bin/node_modules/.bin:${NVM_DIR}/versions/node/v12.19.1/bin:${HOME}/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/usr/bin:/mingw64/bin:/usr/bin:${HOME}/bin:${HOME}/.dnx/bin:/c/Program Files/Microsoft SQL Server/130/Tools:/usr/bin/vendor_perl:/usr/bin/core_perl) which npm: ${NVM_DIR}/versions/node/v12.19.1/bin/npm npm config get prefix: C:\Users\jschilling\.nvm\versions\node\v12.19.1\bin npm root -g: C:\Users\jschilling\.nvm\versions\node\v12.19.1\bin\node_modules ```

nvm ls output:

```sh $ nvm ls v12.19.1 v12.20.2 v16.17.0 v18.13.0 system default -> 18.13.0 (-> v18.13.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v18.13.0) (default) stable -> 18.13 (-> v18.13.0) (default) lts/* -> lts/hydrogen (-> N/A) 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.0 (-> N/A) lts/hydrogen -> v18.16.0 (-> N/A) ```

How did you install nvm?

From the install script in the readme

What steps did you perform?

What happened?

Nvm and the node versions installed as expected, however running the nvm current command returns nothing/blank in a Windows 10 & Git Bash environment. It even returns blank in the nvm debug results posted above.

Also nvm list and nvm ls don't notate the currently selected version of Node from the list of installed versions.

What did you expect to happen?

Similar to how it operates in our MacOS environments, I would expect nvm current to show the currently selected Node version and nvm list and nvm ls to notate the currently selected Node version from the list of installed versions.

Is there anything in any of your profile files that modifies the PATH?

Not that I can see. The .bashrc file alters the NVM_DIR variable and exports it but that is all.

ljharb commented 1 year ago

What happens if you run nvm use?

jschillingApollo commented 1 year ago

What happens if you run nvm use?

Results of nvm use:

$ nvm use
Found '/c/Users/jschilling/.../.nvmrc' with version <18.13.0>
Now using node v18.13.0 (npm v8.19.3)
ljharb commented 1 year ago

That seems to be working properly, at least.

Can you try updating to v0.39.4 and see if that fixes it?

jschillingApollo commented 1 year ago

Updated to v0.39.4 and then subsequently v0.39.5 with a new, but still incorrect, result. nvm current now returns the value none regardless of what version is currently selected and nvm list doesn't show the currently selected version either. nvm use still finds the .nvmrc file in the project and then selects the appropriate version.

$ nvm current
none

nvm debug also returns the same results as originally posted above, except the current line also shows nvm current: none.

ljharb commented 1 year ago

Presumably nvm_ls_current also returns none? if so, what do the following print out?

jschillingApollo commented 11 months ago

It does indeed. I attempted this with a few node versions that I have installed with nvm and saw the same results. Here's the requested output:

$ nvm_ls_current
none

$ command which node
/c/Users/jschilling/.nvm/versions/node/v18.13.0/bin/node

$ nvm_version_dir iojs
/c/Users/jschilling/.nvm/versions/io.js

$ node --version
v18.13.0
ljharb commented 11 months ago

Thanks, that's helpful. What about nvm_tree_contains_path ${NVM_DIR} $(command which node) ; echo $??

jschillingApollo commented 11 months ago

The output from the requested command is:

$ nvm_tree_contains_path ${NVM_DIR} $(command which node) ; echo $?
0
Tobio89 commented 10 months ago

Hello, I'm also facing this issue.

image

I have pm2 running under v17, but I always have to add nvm use 17 before being able to access it, which is a bit of a pain. I haven't made any changes to .nvmrc

Tobio89 commented 10 months ago

I think I found a solution, which was in .bashrc

Broken: PATH=~/bin:/media/sdu1/USER/.nvm/versions/node/v20.10.0/

Working: PATH=~/bin:/media/sdu1/USER/.nvm/versions/node/v17.9.1/

In my case, 20.10.0 wasn't installed, so it couldn't load it.

jschillingApollo commented 10 months ago

I don't have a bin: folder in my path. I didn't think we should be hardcoding what version of node is being used in our .bashrc file.

My .bashrc only has these three lines in relation to nvm

# Load nvm and nvm bash_completion
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

Checking the installed versions confirms the versions listed in the nvm list command are installed (or at least the folders are there);

$ ls ~/.nvm/versions/node/
v12.19.1/  v12.20.2/  v16.17.0/  v18.13.0/

The right version is selected after I run nvm use based on the version in my project's .nvmrc file. I can verify that by running node --version after selecting a version using nvm. The nvm ls command just doesn't indicate the current selected version like it does on macOS and nvm current commands doesn't list anything.

ljharb commented 10 months ago

Indeed, versions shouldn't be hardcoded like that.

I haven't had a chance to look into this yet, but it's clearly a Windows-specific bug that needs fixing. If someone beats me to it, I'd love to review a PR.

ljharb commented 10 months ago

I'm doing some research and this seems like it might be a winpty bug, which is part of git bash.

Specifically:

VERSION="1.2.3"
echo "$VERSION" # prints out 1.2.3

VERSION="$(echo 4.5.6)"
echo "$VERSION" # prints out 4.5.6

node --version 2>/dev/null # prints out whatever the version is
VERSION="$(node --version 2>/dev/null)"
echo "$VERSION" # prints nothing, the var is blank

I'm not sure how to fix this exactly, or how to use -Xallow-non-tty to get this to work.

jschillingApollo commented 10 months ago

I think I found a solution to your example above.

In reference to this SO post, we need to call the node exe explicitly. They mention also that they tried using winpty and the -Xallow-non-tty flag with no success but calling the exe explicitly produced results.

I tested it using the example you posted above (with version 18.13.0 selected in my git bash session).

Results:

node --version # prints v18.13.0

VERSION="$(node --version)"
echo $VERSION # prints nothing

VERSION="$(node.exe --version)"
echo $VERSION # prints v18.13.0

I'm not familiar with the codebase for nvm-sh but if someone knows it better, you might be able to update it quicker. I'll poke around in there to see if I can glean where this type of change would need to go.

ljharb commented 10 months ago

interesting, thanks. i'll see what's achievable there, since it'd have to be done only for git bash