fix(install): repair existing managed-Node global prefix on re-run

The initial fix only wrote the prefix npmrc on a fresh Node install, so
pre-existing bundled-Node installs (Node already present) were not repaired
by re-running the installer — install_node/ensure_node skip when Node is
already up to date.

Extract the redirect into an idempotent helper
(configure_managed_node_npm_prefix / _nb_configure_npm_prefix) that no-ops
when there's no Hermes-managed npm, and call it unconditionally from
check_node (install.sh) and at the top of ensure_node (node-bootstrap.sh).
Re-running the install command now repairs an affected install in place,
not just brand-new ones.
This commit is contained in:
xxxigm 2026-06-14 17:34:11 +07:00
parent 98205da008
commit 1db8f7ea80
3 changed files with 64 additions and 22 deletions

View file

@ -413,6 +413,25 @@ get_command_link_display_dir() {
fi
}
# Point a Hermes-managed Node's `npm install -g` at a directory that is on
# PATH. npm's default global prefix for a bundled Node is the Node dir itself,
# so global package binaries land in $HERMES_HOME/node/bin — which is NOT on
# PATH (only the command link dir is) and is wiped on every Node upgrade.
# Redirecting the prefix to the link dir's parent makes global bins resolve to
# the command link dir (node/npm/npx live there too, already on PATH) and
# survive upgrades. Scoped to the managed Node via its prefix-local global
# npmrc, so the user's other Node installs and their ~/.npmrc are untouched.
# Hermes's own global installs pass an explicit --prefix and are unaffected.
# Idempotent and a no-op when there is no Hermes-managed npm, so calling it on
# every install run repairs pre-existing installs, not just fresh ones.
configure_managed_node_npm_prefix() {
[ -x "$HERMES_HOME/node/bin/npm" ] || return 0
local link_dir
link_dir="$(get_command_link_dir)"
mkdir -p "$HERMES_HOME/node/etc"
printf 'prefix=%s\n' "$(dirname "$link_dir")" > "$HERMES_HOME/node/etc/npmrc"
}
get_hermes_command_path() {
local link_dir
link_dir="$(get_command_link_dir)"
@ -722,6 +741,11 @@ node_satisfies_build() {
check_node() {
log_info "Checking Node.js (for browser tools)..."
# Repair pre-existing Hermes-managed installs where `npm install -g` lands
# off PATH. No-op when there's no managed Node, so this is safe to run on
# every install — including re-runs that skip the Node (re)install below.
configure_managed_node_npm_prefix
if command -v node &> /dev/null && node_satisfies_build "$(node --version)"; then
log_success "Node.js $(node --version) found"
HAS_NODE=true
@ -851,17 +875,7 @@ install_node() {
ln -sf "$HERMES_HOME/node/bin/npm" "$node_link_dir/npm"
ln -sf "$HERMES_HOME/node/bin/npx" "$node_link_dir/npx"
# Point this Node's `npm install -g` at a directory that is actually on
# PATH. By default npm's global prefix is the Node install dir, so user
# globals land in $HERMES_HOME/node/bin — which is NOT on PATH (only the
# link dir is) and is wiped on every Node upgrade. Redirecting the prefix
# to the link dir's parent makes global bins land in the link dir
# (node/npm/npx live there too, and it's already on PATH) and survive
# upgrades. Scoped to this Node via its prefix-local global npmrc, so the
# user's other Node installs and their ~/.npmrc are untouched. Hermes's
# own global installs pass an explicit --prefix and are unaffected.
mkdir -p "$HERMES_HOME/node/etc"
printf 'prefix=%s\n' "$(dirname "$node_link_dir")" > "$HERMES_HOME/node/etc/npmrc"
configure_managed_node_npm_prefix
export PATH="$HERMES_HOME/node/bin:$PATH"