[New] `nvm install --offline`: install from cache without network access
Before adoption: this commit came in with the history the project adopted, so no landing records it.
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
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+208 added−17 removed
- tests:+77 added−0 removed
- other:+131 added−17 removed
Provenance
- Commit
59bd32b- Tree
681bab6- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
14d01c6
4 files changed+208 added−17 removed
added:test/fast/Unit tests/nvm install --offline+38 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+\. ../../../nvm.sh6+7+\. ../../common.sh8+9+# Mock nvm_download to ensure no network access10+nvm_download() {11+ die "nvm_download should not be called in offline mode"12+}13+14+# --offline with an already-installed version should succeed15+INSTALLED_VERSION="$(nvm ls | command tail -1 | command awk '{print $1}' | command sed 's/\x1b\[[0-9;]*m//g')"16+if [ -n "${INSTALLED_VERSION}" ] && [ "_${INSTALLED_VERSION}" != '_N/A' ] && [ "_${INSTALLED_VERSION}" != '_system' ]; then17+ try nvm install --offline "${INSTALLED_VERSION}"18+ [ "_$CAPTURED_EXIT_CODE" = "_0" ] \19+ || die "nvm install --offline with installed version '${INSTALLED_VERSION}' should succeed, got exit code $CAPTURED_EXIT_CODE"20+fi21+22+# --offline with a nonexistent version should fail23+try_err nvm install --offline 999.999.99924+[ "_$CAPTURED_EXIT_CODE" != "_0" ] \25+ || die "nvm install --offline with nonexistent version should fail"26+27+EXPECTED_ERR="not found locally or in cache"28+nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "${EXPECTED_ERR}" \29+ || die "nvm install --offline error should mention 'not found locally or in cache'; got '$CAPTURED_STDERR'"30+31+# --offline should not require curl or wget32+nvm_has() { return 1; }33+try_err nvm install --offline 999.999.99934+# Should fail with "not found" not "nvm needs curl or wget"35+nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "curl or wget" \36+ && die "nvm install --offline should not require curl or wget"37+alias nvm_has='\nvm_has'38+unset -f nvm_hasadded:test/fast/Unit tests/nvm_offline_version+39 added
1+#!/bin/sh2+3+die () { echo "$@" ; cleanup ; exit 1; }4+5+\. ../../../nvm.sh6+7+\. ../../common.sh8+9+TEST_DIR="$(pwd)/nvm_offline_version_tmp"10+11+cleanup() {12+ rm -rf "${TEST_DIR}"13+}14+15+[ ! -e "${TEST_DIR}" ] && mkdir -p "${TEST_DIR}"16+17+# nvm_offline_version should find installed versions18+INSTALLED_VERSION="$(nvm_version node)"19+if [ "_${INSTALLED_VERSION}" != '_N/A' ] && [ "_${INSTALLED_VERSION}" != '_system' ]; then20+ try nvm_offline_version "${INSTALLED_VERSION}"21+ [ "_$CAPTURED_STDOUT" = "_${INSTALLED_VERSION}" ] \22+ || die "nvm_offline_version '${INSTALLED_VERSION}' should return '${INSTALLED_VERSION}'; got '$CAPTURED_STDOUT'"23+ [ "_$CAPTURED_EXIT_CODE" = "_0" ] \24+ || die "nvm_offline_version '${INSTALLED_VERSION}' should exit 0; got '$CAPTURED_EXIT_CODE'"25+fi26+27+# nvm_offline_version with nonexistent version should return N/A28+try nvm_offline_version "999.999.999"29+[ "_$CAPTURED_STDOUT" = "_N/A" ] \30+ || die "nvm_offline_version '999.999.999' should return 'N/A'; got '$CAPTURED_STDOUT'"31+[ "_$CAPTURED_EXIT_CODE" = "_3" ] \32+ || die "nvm_offline_version '999.999.999' should exit 3; got '$CAPTURED_EXIT_CODE'"33+34+# nvm_ls_cached with nonexistent pattern should return nothing35+try nvm_ls_cached "999.999"36+[ -z "$CAPTURED_STDOUT" ] \37+ || die "nvm_ls_cached '999.999' should return empty; got '$CAPTURED_STDOUT'"38+39+cleanupmodified:nvm.sh+115 added-17 removed
2488 unmodified lines2489 2490 local COMPRESSION2491 COMPRESSION="$(nvm_get_artifact_compression "${VERSION}")"2492 2493- local CHECKSUM2494- CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")"2495-2493 local tmpdir2494 if [ "${KIND}" = 'binary' ]; then2495 tmpdir="$(nvm_cache_dir)/bin/${SLUG}"2496 else2497 tmpdir="$(nvm_cache_dir)/src/${SLUG}"2498 fi2499+2500+ local TARBALL2501+ TARBALL="${tmpdir}/${SLUG}.${COMPRESSION}"2502+2503+ if [ "${NVM_OFFLINE-}" = 1 ]; then2504+ # In offline mode, use cached tarball without checksum or download2505+ if [ -r "${TARBALL}" ]; then2506+ nvm_err "Offline: using cached archive $(nvm_sanitize_path "${TARBALL}")"2507+ nvm_echo "${TARBALL}"2508+ return 02509+ fi2510+ nvm_err "Offline: no cached archive found for ${SLUG}"2511+ return 42512+ fi2513+2514+ local CHECKSUM2515+ CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")"2516+2517 command mkdir -p "${tmpdir}/files" || (2518 nvm_err "creating directory ${tmpdir}/files failed"2519 return 32520 )2521 2507- local TARBALL2508- TARBALL="${tmpdir}/${SLUG}.${COMPRESSION}"2522 local TARBALL_URL2523 if nvm_version_greater_than_or_equal_to "${VERSION}" 0.1.14; then2524 TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.${COMPRESSION}"2525 else536 unmodified lines3062 nvm_cache_dir() {3063 nvm_echo "${NVM_DIR}/.cache"3064 }3065 3066+# args: pattern3067+# Lists versions available in the local cache (not yet installed).3068+# Returns version numbers like "v18.20.4", one per line, sorted.3069+nvm_ls_cached() {3070+ local PATTERN3071+ PATTERN="${1-}"3072+ local CACHE_BIN_DIR3073+ CACHE_BIN_DIR="$(nvm_cache_dir)/bin"3074+ if [ ! -d "${CACHE_BIN_DIR}" ]; then3075+ return3076+ fi3077+ local NVM_OS3078+ NVM_OS="$(nvm_get_os)"3079+ local NVM_ARCH3080+ NVM_ARCH="$(nvm_get_arch)"3081+ local SUFFIX3082+ SUFFIX="${NVM_OS}-${NVM_ARCH}"3083+ # shellcheck disable=SC20103084+ command ls -1 "${CACHE_BIN_DIR}" \3085+ | nvm_grep "^node-v.*-${SUFFIX}\$" \3086+ | command sed "s/^node-\\(v[0-9][0-9.]*\\)-${SUFFIX}\$/\\1/" \3087+ | nvm_grep "$(nvm_ensure_version_prefix "${PATTERN}")" \3088+ | command sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n3089+}3090+3091+# args: pattern3092+# Resolves a version pattern to a single version using only locally3093+# installed versions and cached downloads. No network access.3094+nvm_offline_version() {3095+ local PATTERN3096+ PATTERN="${1-}"3097+3098+ # First try locally installed versions3099+ local VERSION3100+ VERSION="$(nvm_version "${PATTERN}")"3101+ if [ "_${VERSION}" != '_N/A' ]; then3102+ nvm_echo "${VERSION}"3103+ return 03104+ fi3105+3106+ # Then try cached downloads3107+ VERSION="$(nvm_ls_cached "${PATTERN}" | command tail -1)"3108+ if [ -n "${VERSION}" ]; then3109+ nvm_echo "${VERSION}"3110+ return 03111+ fi3112+3113+ nvm_echo 'N/A'3114+ return 33115+}3116+3117 nvm() {3118 if [ "$#" -lt 1 ]; then3119 nvm --help3120 return72 unmodified lines3193 nvm_echo ' --lts=<LTS name> When installing, only select from versions for a specific LTS line'3194 nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists'3195 nvm_echo ' --latest-npm After installing, attempt to upgrade to the latest working npm on the given node version'3196 nvm_echo ' --no-progress Disable the progress bar on any downloads'3197+ nvm_echo ' --offline Install from cache only, without downloading anything'3198 nvm_echo ' --alias=<name> After installing, set the alias specified to the version specified. (same as: nvm alias <name> <version>)'3199 nvm_echo ' --default After installing, set default alias to the version specified. (same as: nvm alias default <version>)'3200 nvm_echo ' --save After installing, write the specified version to .nvmrc'3201 nvm_echo ' nvm uninstall <version> Uninstall a version'197 unmodified lines3399 version_not_provided=03400 local NVM_OS3401 NVM_OS="$(nvm_get_os)"3402 3338- if ! nvm_has "curl" && ! nvm_has "wget"; then3339- nvm_err 'nvm needs curl or wget to proceed.'3340- return 13341- fi3342-3403 if [ $# -lt 1 ]; then3404 version_not_provided=13405 fi3406 3407 local nobinary3408 local nosource3409 local noprogress3410+ local NVM_OFFLINE3411 nobinary=03412 noprogress=03413 nosource=03414+ NVM_OFFLINE=03415 local LTS3416 local ALIAS3417 local NVM_UPGRADE_NPM3418 NVM_UPGRADE_NPM=034 unmodified lines3453 --no-progress)3454 noprogress=13455 shift3456 ;;3457+ --offline)3458+ NVM_OFFLINE=13459+ shift3460+ ;;3461 --lts)3462 LTS='*'3463 shift3464 ;;68 unmodified lines3533 ;;3534 esac3535 done3536 3537+ if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has "curl" && ! nvm_has "wget"; then3538+ nvm_err 'nvm needs curl or wget to proceed.'3539+ return 13540+ fi3541+3542 local provided_version3543 provided_version="${1-}"3544 3545 if [ -z "${provided_version}" ]; then32 unmodified lines3578 ;;3579 esac3580 3581 local EXIT_CODE3511- VERSION="$(NVM_VERSION_ONLY=true NVM_LTS="${LTS-}" nvm_remote_version "${provided_version}")"3512- EXIT_CODE="$?"3582 3583+ if [ "${NVM_OFFLINE}" = 1 ]; then3584+ local OFFLINE_PATTERN3585+ OFFLINE_PATTERN="${provided_version}"3586+ if [ -n "${LTS-}" ]; then3587+ if [ "${LTS}" = '*' ]; then3588+ OFFLINE_PATTERN="$(nvm_resolve_alias 'lts/*' 2>/dev/null || nvm_echo)"3589+ else3590+ OFFLINE_PATTERN="$(nvm_resolve_alias "lts/${LTS}" 2>/dev/null || nvm_echo)"3591+ fi3592+ if [ -z "${OFFLINE_PATTERN}" ]; then3593+ nvm_err "LTS alias '${LTS}' not found locally. Run \`nvm ls-remote --lts\` first to populate LTS aliases."3594+ return 33595+ fi3596+ fi3597+ VERSION="$(nvm_offline_version "${OFFLINE_PATTERN}")"3598+ EXIT_CODE="$?"3599+ else3600+ VERSION="$(NVM_VERSION_ONLY=true NVM_LTS="${LTS-}" nvm_remote_version "${provided_version}")"3601+ EXIT_CODE="$?"3602+ fi3603+3604 if [ "${VERSION}" = 'N/A' ] || [ $EXIT_CODE -ne 0 ]; then3605 local LTS_MSG3606 local REMOTE_CMD3607 if [ "${LTS-}" = '*' ]; then6 unmodified lines3614 nvm_err "Version with LTS filter '${LTS}' not found - try \`${REMOTE_CMD}\` to browse available versions."3615 return 33616 fi3617 else3618+ if [ "${NVM_OFFLINE}" = 1 ]; then3619+ REMOTE_CMD='nvm ls'3620+ else3528- REMOTE_CMD='nvm ls-remote'3621+ REMOTE_CMD='nvm ls-remote'3622+ fi3623 fi3624+ if [ "${NVM_OFFLINE}" = 1 ]; then3625+ nvm_err "Version '${provided_version}' ${LTS_MSG-}not found locally or in cache - try \`${REMOTE_CMD}\` to browse available versions."3626+ else3530- nvm_err "Version '${provided_version}' ${LTS_MSG-}not found - try \`${REMOTE_CMD}\` to browse available versions."3627+ nvm_err "Version '${provided_version}' ${LTS_MSG-}not found - try \`${REMOTE_CMD}\` to browse available versions."3628+ fi3629 return 33630 fi3631 3632 ADDITIONAL_PARAMETERS=''131 unmodified lines3764 fi3765 3766 # skip binary install if "nobinary" option specified.3767 if [ $nobinary -ne 1 ] && nvm_binary_available "${VERSION}"; then3670- NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_binary "${FLAVOR}" std "${VERSION}" "${nosource}"3768+ NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" NVM_OFFLINE="${NVM_OFFLINE}" nvm_install_binary "${FLAVOR}" std "${VERSION}" "${nosource}"3769 EXIT_CODE=$?3770 else3771 EXIT_CODE=-13772 if [ $nosource -eq 1 ]; then10 unmodified lines3783 if [ "_${NVM_OS}" = "_win" ]; then3784 nvm_err 'Installing from source on non-WSL Windows is not supported'3785 EXIT_CODE=873786 else3689- NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"3787+ NVM_NO_PROGRESS="${NVM_NO_PROGRESS:-${noprogress}}" NVM_OFFLINE="${NVM_OFFLINE}" nvm_install_source "${FLAVOR}" std "${VERSION}" "${NVM_MAKE_JOBS}" "${ADDITIONAL_PARAMETERS}"3788 EXIT_CODE=$?3789 fi3790 fi3791 fi825 unmodified lines4617 nvm_resolve_alias nvm_ls_current nvm_alias \4618 nvm_binary_available nvm_change_path nvm_strip_path \4619 nvm_num_version_groups nvm_format_version nvm_ensure_version_prefix \4620 nvm_normalize_version nvm_is_valid_version nvm_normalize_lts \4523- nvm_ensure_version_installed nvm_cache_dir \4621+ nvm_ensure_version_installed nvm_cache_dir nvm_ls_cached nvm_offline_version \4622 nvm_version_path nvm_alias_path nvm_version_dir \4623 nvm_find_nvmrc nvm_find_up nvm_find_project_dir nvm_tree_contains_path \4624 nvm_version_greater nvm_version_greater_than_or_equal_to \4625 nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \193 unmodified linesmodified:README.md+16 added
480 unmodified lines481 482 If you've already gotten an error to the effect of "npm does not support Node.js", you'll need to (1) revert to a previous node version (`nvm ls` & `nvm use <your latest _working_ version from the ls>`), (2) delete the newly created node version (`nvm uninstall <your _broken_ version of node from the ls>`), then (3) rerun your `nvm install` with the `--latest-npm` flag.483 484 485+### Offline Install486+487+If you've previously downloaded a node version (or it's still in the cache), you can install it without any network access using the `--offline` flag:488+489+```sh490+nvm install --offline 14.7.0491+```492+493+This resolves versions using only locally installed versions and cached downloads. It will not attempt to download anything. This is useful in air-gapped environments, on planes, or when you want to avoid network latency.494+495+You can combine `--offline` with `--lts` to install the latest cached LTS version (as long as LTS aliases have been populated by a prior `nvm ls-remote --lts`):496+497+```sh498+nvm install --offline --lts499+```500+501 ### Default Global Packages From File While Installing502 503 If you have a list of default packages you want installed every time you install a new version, we support that too -- just add the package names, one per line, to the file `$NVM_DIR/default-packages`. You can add anything npm would accept as a package argument on the command line.504 689 unmodified lines