2

How can I detirmine if nvm (Node Version Manager) is installed in bash?

I already have it installed in my system but I haven't been able to make any bash script that can detect it. I am making a script that should be used by others which depends on nvm, so I want to output if it's not installed and exit if it isn't..

This doesn't work: https://stackoverflow.com/a/26759734/846348 it says that nvm isn't installed but the bash script can use nvm..

Would be nice if it supported Mac Terminal, Mac iTerm and Windows with Linux shell at least.

OZZIE
  • 6,609
  • 7
  • 55
  • 59
  • `nvm` is a actually a shell function and thus the test in the linked answer cannot work since `command -v nvm` doesn't return a file path. – Felix Kling Feb 22 '19 at 07:09

2 Answers2

2

The nvm install script checks if nvm is installed using roughly the following logic:

if [ -d "${HOME}/.nvm/.git" ]; then echo "nvm installed"; else echo "nvm not installed"; fi

This just checks if the directory ~/.nvm/.git exists.

To exit with failure if the directory ~/.nvm/.git does not exist, you could use:

if [ ! -d "${HOME}/.nvm/.git" ]; then exit; fi

Check if nvm installed using a Makefile

NVM_EXISTS := $(shell if [ -d "${HOME}/.nvm/.git" ]; then echo "nvm installed"; fi)

.PHONY: check
check:
ifndef NVM_EXISTS
    $(error Please install nvm: https://github.com/nvm-sh/nvm)
endif

Note on nvm install.sh

The actual install script uses the following functions to determine the nvm installation directory (rather than assuming ${HOME}/.nvm). But if you are using the default location ${HOME}/.nvm, you can skip these checks.

nvm_default_install_dir() {
  [ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
}

nvm_install_dir() {
  if [ -n "$NVM_DIR" ]; then
    printf %s "${NVM_DIR}"
  else
    nvm_default_install_dir
  fi
}
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
1

one can check with command -v nvm:

$ command -v nvm >/dev/null 2>&1 || { echo >&2 "nvm is required, but it's not installed.  Aborting."; exit 1; }
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • which nvm returns empty for me.. which nvm ; echo $? returns 1 – OZZIE Feb 22 '19 at 07:09
  • `command -v nvm >/dev/null 2>&1 || { echo >&2 "nvm is required, but it's not installed. Aborting."; exit 1; } nvm is required, but it's not installed. Aborting. logout Saving session...completed. [Process completed]` – OZZIE Feb 22 '19 at 07:09
  • seems like I have destroyed it somehow now :P – OZZIE Feb 22 '19 at 07:10
  • @OZZIE `which` should not return empty, but should complain it's not on `$PATH` and even list the whole `$PATH`; it's probably not properly installed, when it is not on `$PATH`. `command -v` is the most reliable way to check. – Martin Zeitler Feb 22 '19 at 07:11
  • I added `source ~/.nvm/nvm.sh` to my `~/.bash_profile` and nvm came back,, no idea how I managed to destroy it :P perhaps something I ran somehow removed it from $PATH :S no idea.. – OZZIE Feb 22 '19 at 07:20
  • @MartinZeitler command you suggested to OP returns 1 as status code for me when I use it in script. But when I use it in command line all is OK. Any Idea what I am missing? (sorry for bumping an old thread) – ZuBB Jul 23 '19 at 09:05
  • @ZuBB I don't understand the problem. please see http://tldp.org/LDP/abs/html/exitcodes.html – Martin Zeitler Jul 23 '19 at 11:38
  • Hi @MartinZeitler. In script when I use check mentioned above (with help of `command` shell's built in command I always get `1` as return code and it means that `nvm` executable is not found. while same command run`ned in command prompt produces valid result (0 for case when nvm exists and 1 for case when it does not)). Any idea what can be wrong with my setup? (OS, particular shell or its version, etc) I have tested both on mac+bash3.2 and ubuntu 16.04 LTS + bash 4.4 – ZuBB Jul 24 '19 at 09:49