Home > Back-end >  nvm with WSL for projects stored in Windows
nvm with WSL for projects stored in Windows

Time:10-18

I'm struggling to understand how exactly WSL works.

The training that I'm doing requires nvm so I can manage my node versions in my project, located in my hard drive that is running with Windows.

So, in order to use nvm I need WSL, but I cannot comprehend where it is located. As far as I understood (and I'm pretty sure that I'm wrong) the WSL that I installed is like a Linux VM, completely independent from my Windows, and I operate it through the Ubuntu Terminal that came with WSL. If that's the case, how can nvm in WSL be useful if my project is in my Windows directory and the WSL controls node versions from my Linux VM?

How do I make nvm useful for my project running from my C:\ drive and how does WSL work with nvm in my Windows?

Thank you!

CodePudding user response:

A few options, but each has advantages and disadvantages:

  • If your Node/JavaScript files are on your Windows C:\ drive, then you do have the ability to access that from WSL. Windows paths are automatically mounted and accessible in WSL via /mnt/c, /mnt/d, etc.

    So if you have your JS source in, for example:

    C:\Users\<myusername>\Documents\Projects\JS_class\project1\
    

    Then that would be accessible under Ubuntu in WSL via:

    /mnt/Users/<myusername>/Documents/Projects/JS_class/project1/
    

    The disadvantage here is performance, as mentioned in this question.

  • Alternatively, you can store your project files inside WSL. You can access the WSL path from Windows by using \\wsl$\Ubuntu\home\<yourusername>\ (or similar). You can use this to copy from your existing Windows drive to your WSL distribution.

    Advantage: Faster. Disadvantage: Unknown -- You seem to want the files to be in Windows, but I'm not sure why.

  • Or you can use WSL1 for most Node/JS tasks. See the previously mentioned answer for details on how to convert.

    Advantage: Speed

    Disadvantage: WSL1 hasn't been updated in a while, and is started to run into some compatibility issues. It's still popular enough and stable enough at the moment for me to continue to recommend it, though.

  • There's also a Windows version of nvm as well as a replacement project that the repo there mentions. I cannot personally speak for the quality of it, but it does have 23k stars on Github. Note that it is not affiliated with the original (Linux-based) nvm project.

  • Related