[New] nvm install --offline: install from cache without network access

Add `--offline` flag to `nvm install` that resolves versions using only locally installed versions and cached downloads. No network calls are made.

New helper functions `nvm_ls_cached` and `nvm_offline_version` scan `$NVM_DIR/.cache/bin/` for previously downloaded tarballs.
In offline mode, `nvm_download_artifact` returns cached tarballs directly without checksum verification or download attempts.
The curl/wget requirement is skipped when `--offline` is set.
Supports `--lts` via locally stored LTS alias files.
This commit is contained in:
Jordan Harband
2026-03-13 16:13:19 -04:00
parent 14d01c6877
commit 59bd32be6b
4 changed files with 208 additions and 17 deletions

View File

@@ -0,0 +1,38 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
\. ../../../nvm.sh
\. ../../common.sh
# Mock nvm_download to ensure no network access
nvm_download() {
die "nvm_download should not be called in offline mode"
}
# --offline with an already-installed version should succeed
INSTALLED_VERSION="$(nvm ls | command tail -1 | command awk '{print $1}' | command sed 's/\x1b\[[0-9;]*m//g')"
if [ -n "${INSTALLED_VERSION}" ] && [ "_${INSTALLED_VERSION}" != '_N/A' ] && [ "_${INSTALLED_VERSION}" != '_system' ]; then
try nvm install --offline "${INSTALLED_VERSION}"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] \
|| die "nvm install --offline with installed version '${INSTALLED_VERSION}' should succeed, got exit code $CAPTURED_EXIT_CODE"
fi
# --offline with a nonexistent version should fail
try_err nvm install --offline 999.999.999
[ "_$CAPTURED_EXIT_CODE" != "_0" ] \
|| die "nvm install --offline with nonexistent version should fail"
EXPECTED_ERR="not found locally or in cache"
nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "${EXPECTED_ERR}" \
|| die "nvm install --offline error should mention 'not found locally or in cache'; got '$CAPTURED_STDERR'"
# --offline should not require curl or wget
nvm_has() { return 1; }
try_err nvm install --offline 999.999.999
# Should fail with "not found" not "nvm needs curl or wget"
nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "curl or wget" \
&& die "nvm install --offline should not require curl or wget"
alias nvm_has='\nvm_has'
unset -f nvm_has

View File

@@ -0,0 +1,39 @@
#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
\. ../../../nvm.sh
\. ../../common.sh
TEST_DIR="$(pwd)/nvm_offline_version_tmp"
cleanup() {
rm -rf "${TEST_DIR}"
}
[ ! -e "${TEST_DIR}" ] && mkdir -p "${TEST_DIR}"
# nvm_offline_version should find installed versions
INSTALLED_VERSION="$(nvm_version node)"
if [ "_${INSTALLED_VERSION}" != '_N/A' ] && [ "_${INSTALLED_VERSION}" != '_system' ]; then
try nvm_offline_version "${INSTALLED_VERSION}"
[ "_$CAPTURED_STDOUT" = "_${INSTALLED_VERSION}" ] \
|| die "nvm_offline_version '${INSTALLED_VERSION}' should return '${INSTALLED_VERSION}'; got '$CAPTURED_STDOUT'"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] \
|| die "nvm_offline_version '${INSTALLED_VERSION}' should exit 0; got '$CAPTURED_EXIT_CODE'"
fi
# nvm_offline_version with nonexistent version should return N/A
try nvm_offline_version "999.999.999"
[ "_$CAPTURED_STDOUT" = "_N/A" ] \
|| die "nvm_offline_version '999.999.999' should return 'N/A'; got '$CAPTURED_STDOUT'"
[ "_$CAPTURED_EXIT_CODE" = "_3" ] \
|| die "nvm_offline_version '999.999.999' should exit 3; got '$CAPTURED_EXIT_CODE'"
# nvm_ls_cached with nonexistent pattern should return nothing
try nvm_ls_cached "999.999"
[ -z "$CAPTURED_STDOUT" ] \
|| die "nvm_ls_cached '999.999' should return empty; got '$CAPTURED_STDOUT'"
cleanup