Skip to content

Instantly share code, notes, and snippets.

@noelleleigh
Last active February 8, 2019 21:10
Show Gist options
  • Save noelleleigh/ba34e18b3e0bc4a6a4e93ed7a480536e to your computer and use it in GitHub Desktop.
Save noelleleigh/ba34e18b3e0bc4a6a4e93ed7a480536e to your computer and use it in GitHub Desktop.
Automate the process of upgrading npm from a Node.js installation managed by nvm-windows.
# Automate npm updating when using nvm-windows
# Installs npm@latest for the current active node install
# Source: https://github.com/coreybutler/nvm-windows/issues/300#issuecomment-368192283
$ErrorActionPreference = "Stop"
# Create a folder in the Temp directory and return it
# Source: https://stackoverflow.com/a/34559554/9165387
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
$name = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
# Test for PowerShell version 6 or greater
if ($PSVersionTable.PSVersion.Major -lt 6) {
throw "This script is compatible with PowerShell version >=6.0 (detected $($PSVersionTable.PSVersion.ToString()))."
}
# Get the path to the active node install from nvm
$currentVer = (
nvm list | ForEach-Object {[regex]::Match($_, "\* (\d+\.\d+.\d+)")} |
Where-Object Success | ForEach-Object {"v" + $_.Captures.Groups[1].Value} |
Select-Object -First 1
)
$nvmRoot = (
nvm root | ForEach-Object {[regex]::Match($_, "Current Root: (.+)")} |
Where-Object Success | ForEach-Object {$_.Captures.Groups[1].Value} |
Select-Object -First 1
)
$nodeInstallPath = Join-Path $nvmRoot $currentVer
if (-not (Test-Path $nodeInstallPath)) {
throw [System.IO.FileNotFoundException] "Test-Path : `"$nodeInstallPath`" does not exist."
}
# Create a temp directory and move the node_modules\npm dir into it
$tempDir = New-TemporaryDirectory
Write-Output "Moving `"$(Join-Path $nodeInstallPath "node_modules" "npm")`" to `"$tempDir`"..."
Move-Item $(Join-Path $nodeInstallPath "node_modules" "npm") $tempDir
# Delete the npm and npm.cmd files from the node folder
Write-Output "Removing `"npm`" and `"npm.cmd`" from `"$nodeInstallPath`"..."
Remove-Item $(Join-Path $nodeInstallPath "npm*")
$tempNpmBinDir = Join-Path $tempDir "npm" "bin"
if (-not (Test-Path $tempNpmBinDir)) {
throw [System.IO.FileNotFoundException] "Test-Path : `"$tempNpmBinDir`" does not exist."
}
# Run the install script from the temp folder
Write-Output "Running `"node npm-cli.js i npm@latest -g`" from `"$tempNpmBinDir`"..."
node $(Join-Path $tempNpmBinDir "npm-cli.js") i npm@latest -g
# Delete the temp folder once complete
Write-Output "Cleanup..."
Remove-Item $tempDir -Recurse
@noelleleigh
Copy link
Author

Source: coreybutler/nvm-windows#300 (comment)

Automates the steps outlined by arfaWong in their comment:

I just remove the npm and npm.cmd files from the nodejs location, move dir node_modules/npm from the nodejs location to another location, and run node npm-cli.js i -g npm@latest inside bin dir in the moved folder.

Requires PowerShell Core 6.0 or greater.

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