[Fix] nvm install: apply --alias, --default and --save after every install

`nvm_install_binary` set the alias from its caller's variables,
so a source install or an `$NVM_INSTALL_THIRD_PARTY_HOOK` install never got it,
and no fresh install wrote `.nvmrc`.
Apply both at the end of `nvm install`, after the install is validated.

`--lts` without a version, or `lts/*`, leaves no provided version,
and `nvm alias` treats an empty target as `nvm unalias`:
`nvm install --lts --default` on an installed version removed the default alias.
In that case point the alias at `lts/<name>`, as the default alias already is.
This commit is contained in:
Yashvardhan Thanvi authored and Jordan Harband committed 2026-11-04 00:35:16 +05:30
1 parent 00cc205d6f
commit d190476345
2 files changed
+100 -4

No files matched your search

+20 -4
View File
@@ -3680,9 +3680,6 @@ nvm_install_binary() {
fi
if nvm_install_binary_extract "${NVM_OS}" "${PREFIXED_VERSION}" "${VERSION}" "${TARBALL}" "${TMPDIR}"; then
if [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
fi
return 0
fi
@@ -5340,7 +5337,12 @@ nvm() {
fi
if [ $EXIT_CODE -eq 0 ] && [ -n "${ALIAS-}" ]; then
nvm alias "${ALIAS}" "${provided_version}"
# an LTS install may have no provided version, and an empty target would remove the alias
if [ -z "${provided_version}" ] && [ -n "${LTS-}" ]; then
nvm alias "${ALIAS}" "lts/${LTS}"
else
nvm alias "${ALIAS}" "${provided_version}"
fi
EXIT_CODE=$?
fi
@@ -5453,6 +5455,20 @@ nvm() {
fi
fi
if [ $EXIT_CODE -eq 0 ] && [ $NVM_WRITE_TO_NVMRC -eq 1 ]; then
nvm_write_nvmrc "${VERSION}"
EXIT_CODE=$?
fi
if [ $EXIT_CODE -eq 0 ] && [ -n "${ALIAS-}" ]; then
if [ -z "${provided_version}" ] && [ -n "${LTS-}" ]; then
nvm alias "${ALIAS}" "lts/${LTS}"
else
nvm alias "${ALIAS}" "${provided_version}"
fi
EXIT_CODE=$?
fi
return $EXIT_CODE
;;
"uninstall")