Skip to content

Instantly share code, notes, and snippets.

@joeyguerra
Created October 26, 2024 18:38
Show Gist options
  • Select an option

  • Save joeyguerra/1517efc8eef3d27e02aef154b23a7d43 to your computer and use it in GitHub Desktop.

Select an option

Save joeyguerra/1517efc8eef3d27e02aef154b23a7d43 to your computer and use it in GitHub Desktop.
Uninstall all versions of node managed by nvm, except 23.1
import { execSync } from 'node:child_process'
// Function to execute a shell command and return the output
function execCommand(command) {
return execSync(command, { encoding: 'utf-8' })
}
// List all installed Node.js versions
const installedVersions = execCommand('. ~/.nvm/nvm.sh && nvm ls --no-colors')
.split('\n')
.map(line => line.trim().match(/^v\d+\.\d+\.\d+/))
.filter(Boolean)
.filter(line => line !== 'v23.1')
// Uninstall each version except for 23.1
installedVersions.forEach(version => {
console.log(`Uninstalling: ${version}`)
execCommand(`. ~/.nvm/nvm.sh && nvm uninstall ${version}`);
// console.log(`${version} uninstalled.`);
})
console.log('All versions except for v23.1 have been uninstalled.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment