Skip to content

Instantly share code, notes, and snippets.

@danpetitt
Last active January 27, 2025 02:59

Revisions

  1. danpetitt revised this gist Aug 3, 2020. 1 changed file with 15 additions and 9 deletions.
    24 changes: 15 additions & 9 deletions nvmuse.md
    Original file line number Diff line number Diff line change
    @@ -8,25 +8,31 @@ So the easiest solution to this is to use a simple Powershell command that perfo
    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() {
    if (Test-Path .nvmrc -PathType Any) {
    # 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', '' );
    } else {
    # if no nvmrc use the one passed as argument
    $versionDesired = $args[0]
    }
    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') {
    nvm install $versionDesired x64
    $response = nvm install $versionDesired x64
    } else {
    nvm install $versionDesired x86
    $response = nvm install $versionDesired x86
    }
    nvm use $versionDesired;
    Write-Host $response
    $response = nvm use $versionDesired;
    }
    } else {
    Write-Host "A node version needs adding as an argument if there is no .nvmrc"
    Write-Host $response
    }
    }
    Set-Alias nvmu -value "callnvm"
  2. danpetitt revised this gist Jul 29, 2020. 1 changed file with 18 additions and 8 deletions.
    26 changes: 18 additions & 8 deletions nvmuse.md
    Original file line number Diff line number Diff line change
    @@ -8,15 +8,25 @@ So the easiest solution to this is to use a simple Powershell command that perfo
    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() {
    $versionDesired = $(Get-Content .nvmrc).replace( 'v', '' );
    $response = nvm use $versionDesired;
    if ($response -match 'is not installed') {
    if ($response -match '64-bit') {
    nvm install $versionDesired x64
    } else {
    nvm install $versionDesired x86
    if (Test-Path .nvmrc -PathType Any) {
    $versionDesired = $(Get-Content .nvmrc).replace( 'v', '' );
    } else {
    # if no nvmrc use the one passed as argument
    $versionDesired = $args[0]
    }
    if ($versionDesired -eq "") {
    $response = nvm use $versionDesired;
    if ($response -match 'is not installed') {
    if ($response -match '64-bit') {
    nvm install $versionDesired x64
    } else {
    nvm install $versionDesired x86
    }
    nvm use $versionDesired;
    }
    nvm use $versionDesired;
    } else {
    Write-Host "A node version needs adding as an argument if there is no .nvmrc"
    }
    }
    Set-Alias nvmu -value "callnvm"
  3. danpetitt revised this gist Apr 25, 2020. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions nvmuse.md
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,19 @@ Unfortunately `nvm use` on Windows does not change the node version to that spec
    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 so instead, create an 'alias' to a function that calls the command instead:
    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() {
    nvm use $(Get-Content .nvmrc).replace( 'v', '' );
    $versionDesired = $(Get-Content .nvmrc).replace( 'v', '' );
    $response = nvm use $versionDesired;
    if ($response -match 'is not installed') {
    if ($response -match '64-bit') {
    nvm install $versionDesired x64
    } else {
    nvm install $versionDesired x86
    }
    nvm use $versionDesired;
    }
    }
    Set-Alias nvmu -value "callnvm"
    ```
  4. danpetitt created this gist Apr 25, 2020.
    18 changes: 18 additions & 0 deletions nvmuse.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    # 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: https://github.com/coreybutler/nvm-windows/issues/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 so instead, create an 'alias' to a function that calls the command instead:
    ```
    function callnvm() {
    nvm use $(Get-Content .nvmrc).replace( 'v', '' );
    }
    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.