2

I have nvm for Windows installed and when I do a whereis nvm when logged into my Pengwin Linux distro via WSL2 it shows it's using the binary on the Windows filesystem mount at /mnt/c/Users/seefe/AppData/Roaming/nvm/nvm.exe. This is despite also going through the Linux installation procedure for nvm, which I assumed would hide the Windows version.

It's the same with gatsby-cli. The problem is when in Linux Land, running nvm and gatsby is a chore because the response time for commands across file systems is too slow. What's the WSL environment strategy for ensuring any packages I install via npm when in Linux are installing and using the packages specifically on the Linux filesystem?

Darren Evans
  • 665
  • 10
  • 17
  • 1
    Does this answer your question? [I can't use the npm command on Windows 10 with WSL2 ON (ubuntu terminal)](https://stackoverflow.com/questions/63116206/i-cant-use-the-npm-command-on-windows-10-with-wsl2-on-ubuntu-terminal). I know that's about npm and your question is nvm, but the solution should be the same. – NotTheDr01ds Oct 04 '21 at 16:15
  • So it's a system PATH issue? Still unsure where I need to alter this. Is it in .bashrc or do I need to add config settings via WSL? Editing system paths in Windows is easy but I'm a Linux noob so that's a source of editing confusion if I need to do it in .bashrc. – Darren Evans Oct 04 '21 at 16:25
  • That link does help but still leaves me with questions. I don't want to disable interop completely, I just the situation where if I'm in the Linux distro and run a command that is also available in npm global packages under Windows, it should look first at the Linux file system and then at the Windows appended path. Looking at .bashrc I'm unsure where the Windows path append is happening and also whether I should be asking this question in Linux on how to edit environment variables such as PATH? Anything to avoid cross filesystem sluggishness. Just typing gatsby takes 20 seconds to respond. – Darren Evans Oct 04 '21 at 16:46
  • 1
    Ultimately the solution *is* going to be in the startup files in your shell (e.g. bash). The Windows path is appended by its `/init` process as one of the first things during startup (before `.bashrc`). And since it is *appended*, if something in your startup config *appends* to the path (e.g. `export PATH="$PATH":/path/for/nvm`), the Windows binary can take precedence. If the path to the Linux app is *prepended* (e.g. `export PATH=/path/for/nvm:"$PATH"`), then the Linux app would be found first. – NotTheDr01ds Oct 04 '21 at 17:09
  • 1
    Working on the basis that the solution is to edit Linux $PATH I used ```printenv``` to get a list of Linux environment variables. The path looks like it's prioritising the Linux filesystem: PATH=/home/seefer/.nvm/versions/node/v14.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib... After which comes a host of Windows /mnt/ paths. ```NVM_DIR=/home/seefer/.nvm``` seems okay as do the other NVM_* variables. So why is ```whereis nvm``` telling me it's at a Windows /mnt/ path as mentioned in my question. All very confusing. – Darren Evans Oct 04 '21 at 19:04

1 Answers1

0

Apologies for leading you in the wrong direction with this answer originally.

After installing nvm again in a test WSL session (I usually use n myself) I see that Linux nvm is installed as a bash function, not as a binary. This means that whereis nvm doesn't actually see it, since whereis only looks for files.

> type nvm | head -1
nvm is a function

Two things to note:

  • When entered without a fully-qualified path (absolute or relative), the function will take precedence over a binary. So as long as type nvm returns the function, you should be fine.

    And the fact that you mention (in the comments) that your path starts with /home/seefer/.nvm/versions/node/v14.18.0/bin means that the Linux nvm is working correctly and prepending the correctly activate directory to your path.

    If you are experiencing issues with node or npm after using the nvm use or nvm install commands, let us know.

  • While whereis nvm finds /mnt/c/Users/seefe/AppData/Roaming/nvm/nvm.exe, you would have to actually run nvm.exe for it to run the Windows version.

There is one more thing to watch out for here, though, that is related to that other answer.

npm on Windows is a strange one, since it actually does ship with a shell script (even though it is for Windows) named npm that in turn calls the Windows npm.exe. If you have Windows Node/NPM installed (and it sounds like you do), be careful that you always active the Linux version (so that it is prepended to the path) with the Linux nvm.

Otherwise you can get into issues with Windows npm trying to run under WSL/Linux.

Be careful with this. As long as you have activated a node/npm installation with the Linux

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70