fix(installer): load nvm before Node.js version check

On systems where nvm manages Node.js, the installer was detecting the
system-installed Node (e.g. /usr/bin/node v8) because /usr/bin appears
before ~/.nvm/versions/node/.../bin on PATH in non-interactive shells.

This caused check_node() to fail on the stale system binary and trigger
install_node(), which overwrote the user's nvm environment unnecessarily.

Fix: add try_load_nvm() that sources ~/.nvm/nvm.sh (with --no-use to
avoid side effects) and activates the nvm default alias before check_node
runs. If nvm is not installed, try_load_nvm() is a no-op.

Fixes #49556
This commit is contained in:
Chenglin97 2026-03-18 01:55:12 -07:00
parent 61a19107e1
commit f2c58ff091

View File

@ -1395,6 +1395,21 @@ ensure_default_node_active_shell() {
return 1
}
try_load_nvm() {
# Source nvm before checking Node.js so the installer uses the nvm-managed
# version rather than a system-installed Node that may be too old.
# This is a no-op if nvm is not installed or already loaded.
local nvm_dir="${NVM_DIR:-$HOME/.nvm}"
if [[ -s "${nvm_dir}/nvm.sh" ]]; then
# shellcheck source=/dev/null
. "${nvm_dir}/nvm.sh" --no-use
# Activate the default/current nvm Node version if one is set
if command -v nvm >/dev/null 2>&1; then
nvm use default --silent 2>/dev/null || nvm use node --silent 2>/dev/null || true
fi
fi
}
check_node() {
if command -v node &> /dev/null; then
NODE_VERSION="$(node_major_version || true)"
@ -2340,6 +2355,11 @@ main() {
install_homebrew
# Step 2: Node.js
# Proactively load nvm if available so the installer picks up the nvm-managed
# Node instead of a stale system Node (e.g. /usr/bin/node v8) that appears
# earlier on PATH. Without this, check_node() fails on the system binary and
# install_node() runs unnecessarily, overwriting the user's nvm environment.
try_load_nvm
if ! check_node; then
install_node
fi