nodenv 利用時に nodenv: yarn: command not found エラー発生時の対応方法

よくハマるのでメモ

解消方法

  • nodenv-yarn-install をインストールして、nodenv uninstall {.node-version に記載のバージョン} & nodenv install {.node-version に記載のバージョン} し直すと解消する

  • nodenv-yarn-install

    • node install 時に、yarn も一緒にインストールしてる nodenvプラグイン

作業ログ

$ yarn -v
nodenv: yarn: command not found

The `yarn' command exists in these Node versions:
  12.14.0
$ which yarn
/Users/ユーザー名/.nodenv/shims/yarn
#!/usr/bin/env bash
set -e
[ -n "$NODENV_DEBUG" ] && set -x

program="${0##*/}"
if [ "$program" = "node" ]; then
  for arg; do
    case "$arg" in
    -e* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export NODENV_DIR="${arg%/*}"
        break
      fi
      ;;
    esac
  done
fi

export NODENV_ROOT="/Users/ユーザー名/.nodenv"
exec "/Users/ユーザー名/.nodenv/libexec/nodenv" exec "$program" "$@"
  • /Users/ユーザー名/.nodenv/shims/yarn は下記のようなシェルスクリプト
    • TODO: nodenv-yarn-install を使用しなかった場合に、 nodenv: yarn: command not found になる理由を調べる
#!/usr/bin/env bash
set -e

if [ "$1" = "--debug" ]; then
  export NODENV_DEBUG=1
  shift
fi

if [ -n "$NODENV_DEBUG" ]; then
  export PS4='+ [${BASH_SOURCE##*/}:${LINENO}] '
  set -x
fi

abort() {
  { if [ "$#" -eq 0 ]; then cat -
    else echo "nodenv: $*"
    fi
  } >&2
  exit 1
}

if enable -f "${BASH_SOURCE%/*}"/../libexec/nodenv-realpath.dylib realpath 2>/dev/null; then
  abs_dirname() {
    local path
    path="$(realpath "$1")"
    echo "${path%/*}"
  }
else
  READLINK=$(type -p greadlink readlink 2>/dev/null | head -1)
  [ -n "$READLINK" ] || abort "cannot find readlink - are you missing GNU coreutils?"

  resolve_link() {
    $READLINK "$1"
  }

  abs_dirname() {
    local cwd="$PWD"
    local path="$1"

    while [ -n "$path" ]; do
      cd "${path%/*}"
      local name="${path##*/}"
      path="$(resolve_link "$name" || true)"
    done

    pwd
    cd "$cwd"
  }
fi

if [ -z "${NODENV_ROOT}" ]; then
  NODENV_ROOT="${HOME}/.nodenv"
else
  NODENV_ROOT="${NODENV_ROOT%/}"
fi
export NODENV_ROOT

if [ -z "${NODENV_DIR}" ]; then
  NODENV_DIR="$PWD"
else
  [[ $NODENV_DIR == /* ]] || NODENV_DIR="$PWD/$NODENV_DIR"
  cd "$NODENV_DIR" 2>/dev/null || abort "cannot change working directory to \`$NODENV_DIR'"
  NODENV_DIR="$PWD"
  cd "$OLDPWD"
fi
export NODENV_DIR


shopt -s nullglob

bin_path="$(abs_dirname "$0")"
for plugin_bin in "${NODENV_ROOT}/plugins/"*/bin; do
  PATH="${plugin_bin}:${PATH}"
done
export PATH="${bin_path}:${PATH}"

NODENV_HOOK_PATH="${NODENV_HOOK_PATH}:${NODENV_ROOT}/nodenv.d"
if [ "${bin_path%/*}" != "$NODENV_ROOT" ]; then
  # Add nodenv's own `nodenv.d` unless nodenv was cloned to NODENV_ROOT
  NODENV_HOOK_PATH="${NODENV_HOOK_PATH}:${bin_path%/*}/nodenv.d"
fi
NODENV_HOOK_PATH="${NODENV_HOOK_PATH}:/usr/local/etc/nodenv.d:/etc/nodenv.d:/usr/lib/nodenv/hooks"
for plugin_hook in "${NODENV_ROOT}/plugins/"*/etc/nodenv.d; do
  NODENV_HOOK_PATH="${NODENV_HOOK_PATH}:${plugin_hook}"
done
NODENV_HOOK_PATH="${NODENV_HOOK_PATH#:}"
export NODENV_HOOK_PATH

shopt -u nullglob


command="$1"
case "$command" in
"" )
  { nodenv---version
    nodenv-help
  } | abort
  ;;
-v | --version )
  exec nodenv---version
  ;;
-h | --help )
  exec nodenv-help
  ;;
* )
  command_path="$(command -v "nodenv-$command" || true)"
  if [ -z "$command_path" ]; then
    if [ "$command" == "shell" ]; then
      abort "shell integration not enabled. Run \`nodenv init' for instructions."
    else
      abort "no such command \`$command'"
    fi
  fi

  shift 1
  if [ "$1" = --help ]; then
    if [[ "$command" == "sh-"* ]]; then
      echo "nodenv help \"$command\""
    else
      exec nodenv-help "$command"
    fi
  else
    exec "$command_path" "$@"
  fi
  ;;
esac