Skip to content

Instantly share code, notes, and snippets.

@micky2be
Last active June 13, 2023 20:23
Show Gist options
  • Save micky2be/1b9ae3116322222720ba53aa72798b33 to your computer and use it in GitHub Desktop.
Save micky2be/1b9ae3116322222720ba53aa72798b33 to your computer and use it in GitHub Desktop.

Update-Node-Version

Disclaimer

This script was written for my own use but can be freely be used by others. While bugs reports are welcome, this was not meant to be heavily shared and maintain for wide audiance.

What it is

A PowerShell script using nvm-windows to update node version based on .nvmrc file available. Like nvm-sh, it will traverse directory structure upwards from the current directory looking for the .nvmrc file, then edfault to the one in your profile directory if any.

Reasoning

When working on several different Node projects, potentially using different Node version, it can be annoying to easily switch from one version to another and mostly to the correct one. That what the .nvmrc file is basically for. The author of [nvm-windows] decided not to support such feature and clearly explained where this decision comes from and future alternative is working on. Meanwhile, I created a simple script to allow such desired behavior for myself. Especially since I often switch from Windows to WSL environent, with the latter supporting the .nvmrc file existing in my projects.

Usage

This is meant to be use in a PowerShell 7 terminal and not a Command Prompt. Put the script file in the directory when nvm-window exist (run echo $env:NVM_HOME if not sure). If you have been using nvm, you should be able to access the script from CLI since you are more likely added the directory to your PATH variable. Then simply run Update-Node-Version from your working directory and it should update your Node version using nvm accordingly.

Note

On Unix I run nvm install instead of nvm use as it allows to install the missing version when necessary, then switch to it. If already installed, it simply skip the install, and still switch to the version. To pair with this usage, this script will run in a similar fashion. While 'lts', 'latest', and 'newest' has been tested, I'm not actively using it.

#Requires -Version 7
function Update-Node-Version {
# if `nvm` command is not available, just quite
if (-not (Get-Command nvm -ErrorAction Ignore)) { return }
$FILE = ".nvmrc"
$DEFAULT = Join-Path $HOME $FILE
if (Test-Path $DEFAULT) {
$NODE_VERSION = (Get-Content $DEFAULT) -Replace "v"
}
if (-not((nvm current) -like "No*")) {
$CURRENT_VERSION = (nvm current) -Replace "v"
}
$PATH = $PWD
# Looking for a ".nvmrc" file in current directory and upward
while ($PATH -and -not(Test-Path (Join-Path $PATH $FILE))) {
$PATH = Split-Path $PATH -Parent
}
if ($PATH) {
$PATH = Join-Path $PATH $FILE
$NODE_VERSION = (Get-Content $PATH) -Replace "v"
}
if ($null -eq $NODE_VERSION -or $CURRENT_VERSION -eq $NODE_VERSION) {
return
}
Write-Output ("Found '{0}' with version <{1}>" -f ($PATH ? $PATH : $DEFAULT), $NODE_VERSION)
if ($NODE_VERSION -eq "lts" -or $NODE_VERSION -eq "latest") {
$LATEST, $LTS, $_ = (nvm list available | Select-String -Pattern "[\d]+\.[\d]+\.[\d]+" -AllMatches -List).Matches.Value
if ($NODE_VERSION -eq "latest") {
$NODE_VERSION = $LATEST
} elseif ($NODE_VERSION -eq "lts") {
$NODE_VERSION = $LTS
}
}
# Grab all installed versions
$VERSIONS = (nvm list | Select-String -Pattern "[\d]+\.[\d]+\.[\d]+").Matches
# Grab the latest installed version matching the one requested (versions are ordered)
if ($VERSIONS -and $NODE_VERSION -ne "newest") {
$VERSIONS = ($VERSIONS.Value | Select-String -Pattern "^${NODE_VERSION}.*").Matches
}
$NEWEST = $VERSIONS ? $VERSIONS[0].Value : $null
# We are already using the requested version
if ($null -ne $CURRENT_VERSION -and $CURRENT_VERSION -eq $NEWEST) {
Write-Output ("Already using <{0}>, no need to switch." -f $CURRENT_VERSION)
return
}
if ($null -eq $NEWEST) {
Write-Output ("Version <{0}> not found locally. Running install." -f $NODE_VERSION)
nvm install $NODE_VERSION
}
nvm use $NODE_VERSION
}
Update-Node-Version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment