Skip to content
Myr

[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.

authored by Jordan Harband · committed by Jordan Harband · parent 14d01c6 · Browse files · 4 files+208 added−17 removed
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

4 files+208 added−17 removed

added:test/fast/Unit tests/nvm install --offline+38 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45\. ../../../nvm.sh67\. ../../common.sh89# Mock nvm_download to ensure no network access10nvm_download() {11 die "nvm_download should not be called in offline mode"12}1314# --offline with an already-installed version should succeed15INSTALLED_VERSION="$(nvm ls | command tail -1 | command awk '{print $1}' | command sed 's/\x1b\[[0-9;]*m//g')"16if [ -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"20fi2122# --offline with a nonexistent version should fail23try_err nvm install --offline 999.999.99924[ "_$CAPTURED_EXIT_CODE" != "_0" ] \25 || die "nvm install --offline with nonexistent version should fail"2627EXPECTED_ERR="not found locally or in cache"28nvm_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'"3031# --offline should not require curl or wget32nvm_has() { return 1; }33try_err nvm install --offline 999.999.99934# Should fail with "not found" not "nvm needs curl or wget"35nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "curl or wget" \36 && die "nvm install --offline should not require curl or wget"37alias nvm_has='\nvm_has'38unset -f nvm_has
added:test/fast/Unit tests/nvm_offline_version+39 added
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45\. ../../../nvm.sh67\. ../../common.sh89TEST_DIR="$(pwd)/nvm_offline_version_tmp"1011cleanup() {12 rm -rf "${TEST_DIR}"13}1415[ ! -e "${TEST_DIR}" ] && mkdir -p "${TEST_DIR}"1617# nvm_offline_version should find installed versions18INSTALLED_VERSION="$(nvm_version node)"19if [ "_${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'"25fi2627# nvm_offline_version with nonexistent version should return N/A28try 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'"3334# nvm_ls_cached with nonexistent pattern should return nothing35try nvm_ls_cached "999.999"36[ -z "$CAPTURED_STDOUT" ] \37 || die "nvm_ls_cached '999.999' should return empty; got '$CAPTURED_STDOUT'"3839cleanup
modified:nvm.sh+115 added-17 removed
2488 unmodified lines
24892490 local COMPRESSION2491 COMPRESSION="$(nvm_get_artifact_compression "${VERSION}")"24922493 local CHECKSUM2494 CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")"24952493 local tmpdir2494 if [ "${KIND}" = 'binary' ]; then2495 tmpdir="$(nvm_cache_dir)/bin/${SLUG}"2496 else2497 tmpdir="$(nvm_cache_dir)/src/${SLUG}"2498 fi24992500 local TARBALL2501 TARBALL="${tmpdir}/${SLUG}.${COMPRESSION}"25022503 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 fi25132514 local CHECKSUM2515 CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")"25162517 command mkdir -p "${tmpdir}/files" || (2518 nvm_err "creating directory ${tmpdir}/files failed"2519 return 32520 )25212507 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 else
536 unmodified lines
3062nvm_cache_dir() {3063 nvm_echo "${NVM_DIR}/.cache"3064}30653066# args: pattern3067# Lists versions available in the local cache (not yet installed).3068# Returns version numbers like "v18.20.4", one per line, sorted.3069nvm_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}30903091# args: pattern3092# Resolves a version pattern to a single version using only locally3093# installed versions and cached downloads. No network access.3094nvm_offline_version() {3095 local PATTERN3096 PATTERN="${1-}"30973098 # First try locally installed versions3099 local VERSION3100 VERSION="$(nvm_version "${PATTERN}")"3101 if [ "_${VERSION}" != '_N/A' ]; then3102 nvm_echo "${VERSION}"3103 return 03104 fi31053106 # Then try cached downloads3107 VERSION="$(nvm_ls_cached "${PATTERN}" | command tail -1)"3108 if [ -n "${VERSION}" ]; then3109 nvm_echo "${VERSION}"3110 return 03111 fi31123113 nvm_echo 'N/A'3114 return 33115}31163117nvm() {3118 if [ "$#" -lt 1 ]; then3119 nvm --help3120 return
72 unmodified lines
3193 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 lines
3399 version_not_provided=03400 local NVM_OS3401 NVM_OS="$(nvm_get_os)"34023338 if ! nvm_has "curl" && ! nvm_has "wget"; then3339 nvm_err 'nvm needs curl or wget to proceed.'3340 return 13341 fi33423403 if [ $# -lt 1 ]; then3404 version_not_provided=13405 fi34063407 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=0
34 unmodified lines
3453 --no-progress)3454 noprogress=13455 shift3456 ;;3457 --offline)3458 NVM_OFFLINE=13459 shift3460 ;;3461 --lts)3462 LTS='*'3463 shift3464 ;;
68 unmodified lines
3533 ;;3534 esac3535 done35363537 if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has "curl" && ! nvm_has "wget"; then3538 nvm_err 'nvm needs curl or wget to proceed.'3539 return 13540 fi35413542 local provided_version3543 provided_version="${1-}"35443545 if [ -z "${provided_version}" ]; then
32 unmodified lines
3578 ;;3579 esac35803581 local EXIT_CODE3511 VERSION="$(NVM_VERSION_ONLY=true NVM_LTS="${LTS-}" nvm_remote_version "${provided_version}")"3512 EXIT_CODE="$?"35823583 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 fi36033604 if [ "${VERSION}" = 'N/A' ] || [ $EXIT_CODE -ne 0 ]; then3605 local LTS_MSG3606 local REMOTE_CMD3607 if [ "${LTS-}" = '*' ]; then
6 unmodified lines
3614 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 fi36313632 ADDITIONAL_PARAMETERS=''
131 unmodified lines
3764 fi37653766 # 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 ]; then
10 unmodified lines
3783 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 fi
825 unmodified lines
4617 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 lines
modified:README.md+16 added
480 unmodified lines
481482If 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.483484485### Offline Install486487If 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:488489```sh490nvm install --offline 14.7.0491```492493This 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.494495You 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`):496497```sh498nvm install --offline --lts499```500501### Default Global Packages From File While Installing502503If 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