Skip to main content

nvs: One Node Version Per Terminal in Windows

· 3 min read

I tried setting up the better known nvm-windows to automatically change the node version. But unsuccessfully!

When I used to run nvm use the node version changed globally.

I have read about nvs: Automatic Switching Per Directory (github.com) in nvs readme file and decided to give it a try.

Remove nvm-windows

Go to C:\Users\[username]\AppData\Roaming\nvm and run unins000.exe by double clicking it.

Install nvs

I downloaded the nvs-1.6.0.msi version of nvs and then ran this file by double clicking on it.

Adding node versions

To add a specific node version, run nvs add with the version number:

❯ nvs add 12.21.0
Adding: node/12.21.0/x64
Extracting [###########################] 100%
PATH += $env:LOCALAPPDATA\nvs\node\12.21.0\x64

Setting the default node version

Run nvs link with the version number:

nvs link 12.21.0

Setup Git-Bash

note

Git-Bash is my fallback. It allows the use Unix command sintax.

Add to C:\Users\[username]\.bash_profile these two lines:

.bash_profile
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

Add to C:\Users\[username]\.bashrc these lines (if this file is missing, create it in notepad):

.bashrc
function setupNvs {
export NVS_HOME="$LOCALAPPDATA\\nvs";
[ -s "$NVS_HOME/nvs.sh" ] && source "$NVS_HOME/nvs.sh" >> /dev/null;
return 0;
}
setupNvs
caution

Look at th line export NVS_HOME="$LOCALAPPDATA\\nvs"; this is where windows env (%LOCALAPPDATA%\nvs) is converted to unix env. In case of error see for example: superuser.com: How To Use Localappdata With Git Bash In Windows

nvs Basic Usage

Open PowerShell or Git-Bash terminal.

Let's create two project folders with two different .nvmrc files using these commands:

cd ~
mkdir one
cd one
"10.24.0" | Out-File .nvmrc # create .nvmrc file with node version
[string]::Join( "`n", (gc .nvmrc)) | sc .nvmrc # convert the line above from Windows to Unix

cd ~
mkdir two
cd two
"14.16.0" | Out-File .nvmrc # create .nvmrc file with node version
[string]::Join( "`n", (gc .nvmrc)) | sc .nvmrc # convert the line above from Windows to Unix

Manual switching using .nvmrc

Let's go to the project one and run nvs auto (in PowerShell or Git-Bash):

❯ nvs auto
PATH -= $env:LOCALAPPDATA\nvs\default
PATH += $env:LOCALAPPDATA\nvs\node\10.24.0\x64
note

If everything is ok, nvs will automatically download the correct node version and activate it.

caution

In case of an error, you may need to edit or replace this line:

PowerShell
[string]::Join( "`n", (gc .nvmrc)) | sc .nvmrc

source: stackoverflow.com: Unix Format Files With Powershell

Then open second terminal, go to the project two folder and again run nvs auto:

❯ nvs auto
PATH -= $env:LOCALAPPDATA\nvs\default
PATH += $env:LOCALAPPDATA\nvs\node\14.16.0\x64

Again, nvs install the required node version and then change the environment variable to use it.

Check Switching Ability

Check the version of node in the folder one (in first terminal):

❯ node -v
v10.24.0

Check the node version in the folder two (in second terminal):

❯ node -v
14.26.0
tip

Hence, nvs changes the node versions locally, one node version per terminal.

Which was required to check.