Skip to content

Instantly share code, notes, and snippets.

@GoldenMaximo
Last active May 2, 2026 21:14
Show Gist options
  • Select an option

  • Save GoldenMaximo/e1cd8b98d20012800e202d795c7190f0 to your computer and use it in GitHub Desktop.

Select an option

Save GoldenMaximo/e1cd8b98d20012800e202d795c7190f0 to your computer and use it in GitHub Desktop.
Automatically reads .nvmrc in PowerShell 7 when using nvm use

Open your profile (code $PROFILE) and add:

function nvm {
    if ($args[0] -eq 'use' -and $args.Count -eq 1) {
        $nvmrcPath = Join-Path (Get-Location) '.nvmrc'
        if (-not (Test-Path $nvmrcPath)) {
            Write-Host "No .nvmrc found in current directory." -ForegroundColor Red
            return
        }

        $requestedVersion = (Get-Content $nvmrcPath -Raw).Trim().TrimStart('v')
        if ([string]::IsNullOrWhiteSpace($requestedVersion)) {
            Write-Host ".nvmrc is empty." -ForegroundColor Red
            return
        }

        $installedVersions = & nvm.exe list 2>$null
        $isInstalled = $installedVersions | Select-String -Pattern ([regex]::Escape($requestedVersion)) -Quiet

        if (-not $isInstalled) {
            Write-Host "nvm: Node $requestedVersion not installed. Installing..." -ForegroundColor Yellow
            & nvm.exe install $requestedVersion
        }

        & nvm.exe use $requestedVersion
    } else {
        & nvm.exe @args
    }
}

How it works

  • nvm use (no args) → reads .nvmrc, installs if needed, switches.
  • nvm use 22, nvm install, nvm list, etc. → passed straight through to nvm.exe as normal.
  • No prompt hook, no running on every command.

The key trick is calling nvm.exe explicitly inside the function to avoid infinite recursion — since the function itself is named nvm.

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