diff --git a/scripts/install.sh b/scripts/install.sh index 7d644fe2d77..030d57d4c14 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -851,6 +851,18 @@ 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" + export PATH="$HERMES_HOME/node/bin:$PATH" local installed_ver diff --git a/scripts/lib/node-bootstrap.sh b/scripts/lib/node-bootstrap.sh index 02e568733f3..15763d70486 100644 --- a/scripts/lib/node-bootstrap.sh +++ b/scripts/lib/node-bootstrap.sh @@ -206,6 +206,14 @@ _nb_install_bundled_node() { ln -sf "$HERMES_HOME/node/bin/node" "$_link_dir/node" ln -sf "$HERMES_HOME/node/bin/npm" "$_link_dir/npm" ln -sf "$HERMES_HOME/node/bin/npx" "$_link_dir/npx" + + # Redirect this Node's `npm install -g` to the link dir (already on PATH) + # instead of the default $HERMES_HOME/node/bin, which is off PATH and wiped + # on every Node upgrade. Scoped to this Node via its prefix-local global + # npmrc; the user's other Node installs / ~/.npmrc are untouched. + mkdir -p "$HERMES_HOME/node/etc" + printf 'prefix=%s\n' "$(dirname "$_link_dir")" > "$HERMES_HOME/node/etc/npmrc" + export PATH="$HERMES_HOME/node/bin:$PATH" _nb_have_modern_node || return 1