Skip to content

Instantly share code, notes, and snippets.

@danpetitt
Last active July 10, 2024 01:22
Show Gist options
  • Save danpetitt/e87dabb707079e1bfaf797d0f5f798f2 to your computer and use it in GitHub Desktop.
Save danpetitt/e87dabb707079e1bfaf797d0f5f798f2 to your computer and use it in GitHub Desktop.
Using nvmrc on Windows

Using nvmrc on Windows

Unfortunately nvm use on Windows does not change the node version to that specified in the .nvmrc file as its not supported on nvm for Windows: coreybutler/nvm-windows#388

So the easiest solution to this is to use a simple Powershell command that performs an approximation of the command as follows: nvm use $(Get-Content .nvmrc).replace( 'v', '' );

However, thats a bit awkward and we can do a bit more so instead, we can create an 'alias' to a function that calls the command instead:

function callnvm() {
  # Always use argument version if there is one
  $versionDesired = $args[0]

  if (($versionDesired -eq "" -Or $versionDesired -eq $null) -And (Test-Path .nvmrc -PathType Any)) {
    # if we have an nvmrc and no argument supplied, use the version in the file
    $versionDesired = $(Get-Content .nvmrc).replace( 'v', '' );
  }
  Write-Host "Requesting version '$($versionDesired)'"

  if ($versionDesired -eq "") {
    Write-Host "A node version needs specifying as an argument if there is no .nvmrc"
  } else {
    $response = nvm use $versionDesired;
    if ($response -match 'is not installed') {
      if ($response -match '64-bit') {
        $response = nvm install $versionDesired x64
      } else {
        $response = nvm install $versionDesired x86
      }
      Write-Host $response

      $response = nvm use $versionDesired;
    }

    Write-Host $response
  }
}
Set-Alias nvmu -value "callnvm"

Now we only need to type nvmu in a project folder for it to work properly.

That will only work for the current session, so to make it more globally useful, we can add this content to the Powershell Profile for the current user. You can get the location of this file by typing $profile in a Powershell session and either edit or create the file and place the content above into it.

@MidasVE
Copy link

MidasVE commented Jun 27, 2022

Hi @danpetitt this is a great script! The only issue I get is that I always need an elevated window to run it due to my access right. Is there any way to make it so it automatically calls it from an elevated state if necessary? I've tried editing your script with these examples, with little success.. https://stackoverflow.com/questions/7690994/running-a-command-as-administrator-using-powershell
Thanks!

@nelson6e65
Copy link

@MidasVE The default directory for nodejs link is C:\Program Files\nodejs, which requires elevation in order to be modified. You can use another directory, like C:\bin\nodejs, instead.

@bogdankorshunov
Copy link

bogdankorshunov commented Mar 15, 2023

If it is necessary to switch node versions automatically in git bash with yarn, use
alias dev='powershell -Command "nvm use (Get-Content .nvmrc); yarn dev"' in .bashrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment