[New] nvm install: add NVM_NO_SOURCE_FALLBACK to disable the source fallback

By default a failed binary download falls back to compiling node from source,
which is slow and pointless on platforms that always have prebuilt binaries.
`-b` already disables the fallback per-invocation;
`NVM_NO_SOURCE_FALLBACK=1` makes it the default for every `nvm install`,
so CI images and dev setups need not thread `-b` through every callsite.

It behaves exactly like `-b`
- aborting with a non-zero exit on a failed binary download
- and is mutually exclusive with `-s`, for the same reason `-b` is.
This commit is contained in:
Jordan Harband
2026-07-24 10:54:17 -07:00
parent aee1f83f0f
commit 9570a22bfe
3 changed files with 66 additions and 0 deletions
+4
View File
@@ -872,6 +872,10 @@ nvm exposes the following environment variables:
Additionally, nvm modifies `PATH`, and, if present, `MANPATH` and `NODE_PATH` when changing versions. Additionally, nvm modifies `PATH`, and, if present, `MANPATH` and `NODE_PATH` when changing versions.
The following environment variables can be set to configure `nvm install`:
- `NVM_NO_SOURCE_FALLBACK` - when `1`, a failed binary download aborts instead of silently falling back to a (much slower) from-source compile; the persistent equivalent of the `-b` flag, and mutually exclusive with `-s`.
## Bash Completion ## Bash Completion
+12
View File
@@ -3362,6 +3362,7 @@ nvm() {
nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm install`:' nvm_echo ' The following optional arguments, if provided, must appear directly after `nvm install`:'
nvm_echo ' -s Skip binary download, install from source only.' nvm_echo ' -s Skip binary download, install from source only.'
nvm_echo ' -b Skip source download, install from binary only.' nvm_echo ' -b Skip source download, install from binary only.'
nvm_echo ' (set NVM_NO_SOURCE_FALLBACK=1 to make this the default for every install)'
nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>' nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>'
nvm_echo ' --lts When installing, only select from LTS (long-term support) versions' nvm_echo ' --lts When installing, only select from LTS (long-term support) versions'
nvm_echo ' --lts=<LTS name> When installing, only select from versions for a specific LTS line' nvm_echo ' --lts=<LTS name> When installing, only select from versions for a specific LTS line'
@@ -3710,6 +3711,17 @@ nvm() {
esac esac
done done
# NVM_NO_SOURCE_FALLBACK=1 makes `nvm install` behave as if `-b` were always passed:
# a failed binary download aborts instead of silently falling back to a from-source compile.
# It is a persistent policy so callers need not thread `-b` through every invocation.
if [ "${NVM_NO_SOURCE_FALLBACK-}" = '1' ] && [ $nosource -ne 1 ]; then
if [ $nobinary -eq 1 ]; then
nvm_err '-s cannot be combined with NVM_NO_SOURCE_FALLBACK=1 since that would skip install from both binary and source'
return 6
fi
nosource=1
fi
if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has_executable "curl" && ! nvm_has_executable "wget"; then if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has_executable "curl" && ! nvm_has_executable "wget"; then
nvm_err 'nvm needs curl or wget to proceed.' nvm_err 'nvm needs curl or wget to proceed.'
return 1 return 1
+50
View File
@@ -0,0 +1,50 @@
#!/bin/sh
cleanup() {
[ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"
unset -f die cleanup nvm_remote_version nvm_has_executable nvm_binary_available \
nvm_install_binary nvm_install_source nvm_get_make_jobs
unset NVM_DIR version SOURCE_MARKER OUTPUT EXIT_CODE
}
die() { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
unset npm_config_prefix NPM_CONFIG_PREFIX
NVM_DIR="$(mktemp -d)"
[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'
version='v20.0.0'
SOURCE_MARKER="${NVM_DIR}/source-was-attempted"
# Stay offline; make the binary download always fail and record any attempt to
# compile from source.
nvm_remote_version() { nvm_echo "${version}"; }
nvm_has_executable() { return 0; }
nvm_binary_available() { return 0; }
nvm_get_make_jobs() { NVM_MAKE_JOBS=1; }
nvm_install_binary() { return 1; }
nvm_install_source() { command touch "${SOURCE_MARKER}"; return 1; }
# Default: a failed binary download falls back to a source compile.
rm -f "${SOURCE_MARKER}"
nvm install "${version}" > /dev/null 2>&1
[ -f "${SOURCE_MARKER}" ] || die 'default behaviour should fall back to source when the binary fails'
# NVM_NO_SOURCE_FALLBACK=1: a failed binary download must NOT compile from source.
rm -f "${SOURCE_MARKER}"
NVM_NO_SOURCE_FALLBACK=1 nvm install "${version}" > /dev/null 2>&1; EXIT_CODE=$?
[ ! -f "${SOURCE_MARKER}" ] || die 'NVM_NO_SOURCE_FALLBACK=1 should not fall back to source'
[ "${EXIT_CODE}" != "0" ] || die 'a failed binary install with NVM_NO_SOURCE_FALLBACK=1 should be non-zero'
# NVM_NO_SOURCE_FALLBACK=1 combined with -s would skip both methods: a conflict.
OUTPUT="$(NVM_NO_SOURCE_FALLBACK=1 nvm install -s "${version}" 2>&1)"; EXIT_CODE=$?
[ "${EXIT_CODE}" = "6" ] || die "expected exit code 6 for -s with NVM_NO_SOURCE_FALLBACK=1, got ${EXIT_CODE}"
case "${OUTPUT}" in
*'skip install from both binary and source'*) ;;
*) die "expected a conflict message, got >${OUTPUT}<" ;;
esac
cleanup