Search code examples
node.jswindows-subsystem-for-linuxnvm

When installing nvm on WSL2, can I use it in Windows itself?


I want to install node.js on my Windows machine and I want to use nvm to do so. The only way to install the actual nvm on a windows machine that I'm aware of is using WSL (I am currently considering WSL2 but if there is a reason to use 1 let me know, I am not experienced). As far as I understand WSL is somewhat similar to (or is?) a virtual machine, in which case installing nvm in WSL only makes sense if I want to use node.js in WSL as well. Or am I wrong and there is some magic at work which makes Windows itself able to use nvm which was installed in WSL?


In short:

nvm on WSL -> node.js on WSL

or

nvm on WSL -> node.js on Windows


I am aware that WSL only allows me to install some linux distribution. Only for the sake of argument did I say "install on WSL".


Solution

  • You are correct that the only supported method of running nvm on Windows is through WSL. There is a separate (unrelated to the "original" nvm) nvm-windows project, but I can't speak to the quality of it. On the "pro" side, I do see that Microsoft references it in some of their NodeJS doc for Windows. But as a "con", it looks like it was last updated somewhere around 3 years ago. That may or may not be (or become) a problem.

    And yes, if you use nvm under WSL, it is only going to install the Linux version. You cannot use it to manage Node-for-Windows installations. And as @ThomasUrban pointed out in the comments, having a Windows node version already installed may even cause issues when trying to install nvm.

    I prefer n, personally, but it will have similar limitations as well.

    It's really a separate question, which probably should have been posted separately, but WSL2 and WSL1 do complement each other. I'm going to copy/paste from a Reddit post I did a while back:

    • Advantages of WSL2 over WSL1:
      • Real Linux kernel which allows for features like Docker to be run natively. WSL1, on the other hand, emulates Linux APIs by attempting to map them to an equivalent Windows API.
      • Real networking stack which allows for VPNs, tunneling, etc.
      • Slightly better ext4 performance over WSL1
      • More robust filesystem through the use of a virtual HDD (.vhdx)
    • Disadvantages of WSL2 as compared to WSL1:
      • MUCH slower NTFS/drvfs performance. Seriously, a git clone of the WSL kernel repo takes seconds on WSL1, but around 10 minutes on WSL2. If you need to work with files on the NTFS, keep a WSL1 instance around for this.

      • The network is on a virtual NIC "behind" the Windows host. This means that if you run a service (e.g. ssh or http) on the WSL2 instance, it will not be available to other devices on your network without some hacky port-forwarding.

        WSL1's networking stack is pseudo-bridged, so network services are accessible directly from the Windows host IP address.

        As a result, if you are using WSL for something like React/Angular/Svelte/Vue/etc development, WSL1 is more straightforward from a networking perspective.

    So perhaps surprisingly, for a lot of Node developers, WSL1 might be the better option. You can always install a second copy and make it WSL2 - Both will run alongside each other.

    And one final note:

    I am aware that WSL only allows me to install some Linux distributions.

    You might be surprised. While there are a few distributions pre-packaged in the Microsoft Store, WSL is able to import just about any (Microsoft says flat-out "any") distribution. See this doc.