Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@othyn
Created December 4, 2021 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save othyn/f5aa2346b5f16d74b57b331710f72dbf to your computer and use it in GitHub Desktop.
Save othyn/f5aa2346b5f16d74b57b331710f72dbf to your computer and use it in GitHub Desktop.
Easy ZSH/Bash alias for swapping Node versions on the fly
# Switch Node version in brew
# Place this in your ~/.bashrc or similar for autoload into your shell
function nodesw() {
if [ -z "$1" ]; then
logInfo "You need to provide a Node formulae version to switch to in format:"
logInfo "$ nodesw 16"
if command -v jq >/dev/null 2>&1; then
logInfo "Installed Node versions via Brew:"
brew info --json node | jq -r '.[].versioned_formulae[]'
else
logInfo "Please install jq to list installed Node versions via: $ brew install jq"
fi
return 1
fi
local nodeFormulaeVersion="node@$1"
if brew ls --versions $nodeFormulaeVersion > /dev/null; then
logInfo "${nodeFormulaeVersion} is already installed, continuing..."
else
logInfo "${nodeFormulaeVersion} is not installed, installing..."
brew install $nodeFormulaeVersion
logInfo "${nodeFormulaeVersion} installed!"
fi
logInfo "Now linking ${nodeFormulaeVersion} and making it active!"
brew unlink node && brew link --force --overwrite $nodeFormulaeVersion
logInfo "Done! ${nodeFormulaeVersion} is now active:"
node -v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment