Skip to content

Commit 5dc31ac

Browse files
r4reetikljharb
authored andcommitted
[patch] give a more helpful message when lts alias is mistakenly used
1 parent da2720a commit 5dc31ac

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

nvm.sh

+6-4
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,12 @@ nvm_ensure_version_installed() {
700700
nvm_err "N/A: version \"${PREFIXED_VERSION:-$PROVIDED_VERSION}\" is not yet installed."
701701
fi
702702
nvm_err ""
703-
if [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then
704-
nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it."
705-
else
706-
nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.'
703+
if [ "${PROVIDED_VERSION}" = 'lts' ]; then
704+
nvm_err '`lts` is not an alias - you may need to run `nvm install --lts` to install and `nvm use --lts` to use it.'
705+
elif [ "${IS_VERSION_FROM_NVMRC}" != '1' ]; then
706+
nvm_err "You need to run \`nvm install ${PROVIDED_VERSION}\` to install and use it."
707+
else
708+
nvm_err 'You need to run `nvm install` to install and use the node version specified in `.nvmrc`.'
707709
fi
708710
return 1
709711
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
die () { echo "$@" ; exit 1; }
6+
7+
\. ../../../nvm.sh
8+
9+
# Deactivate any active node version
10+
nvm deactivate >/dev/null 2>&1 || die 'deactivate failed'
11+
12+
# Attempt to use 'lts' without '--' and capture the error message
13+
ERROR_OUTPUT=$(nvm use lts 2>&1) || true
14+
15+
EXPECTED_ERROR='`lts` is not an alias - you may need to run `nvm install --lts` to install and `nvm use --lts` to use it.'
16+
17+
# Check if the error message matches the expected output
18+
echo "$ERROR_OUTPUT" | grep -q "$EXPECTED_ERROR" \
19+
|| die "Expected error message not found. Got: $ERROR_OUTPUT"

0 commit comments

Comments
 (0)