Repository: nvm-sh/nvm
File: nvm.sh (Shell)

Code before:
        # Read lines from $NVM_DIR/default-packages
        local line
        while IFS=" " read -r line; do

          # Skip empty lines.
          [ -z "${line}" ] || continue

          # Skip comment lines that begin with `#`.

          [ "$($line | head -c 1)" != "#" ] || continue

          # Fail on lines that have multiple space-separated words
          case ${VAR} in
            *\ * )
              nvm_err "Only one package per line is allowed in the ${NVM_DIR}/default-packages file. Please remove any lines with multiple space-seperated values."
              return 1
            ;;
          esac

          DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "
        done < "${NVM_DIR}/default-packages"

        if [ -z "$DEFAULT_PACKAGES" ]; then
          nvm_echo "Installing default global packages from ${NVM_DIR}/default-packages..."
          { 
            nvm_echo "$DEFAULT_PACKAGES" | command xargs npm install -g --quiet 
          } || {
            nvm_err "Failed installing default packages. Please check if your default-packages file or a package in it has problems!"
            return 1
          }
        fi
      fi

      if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then
        nvm_err "You can't reinstall global packages from the same version of node you're installing."
        return 4
      elif [ "${REINSTALL_PACKAGES_FROM-}" = 'N/A' ]; then
        nvm_err "If --reinstall-packages-from is provided, it must point to an installed version of node."
        return 5
      fi

      local FLAVOR
      if nvm_is_iojs_version "$VERSION"; then
        FLAVOR="$(nvm_iojs_prefix)"
      else
        FLAVOR="$(nvm_node_prefix)"
      fi

      if nvm_is_version_installed "$VERSION"; then
        nvm_err "$VERSION is already installed."
        if nvm use "$VERSION" && [ ! -z "$REINSTALL_PACKAGES_FROM" ] && [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; then

Review comment (refactor):
ah - you're trying to `npm install` before `nvm use` is ran on line 2488.

I think you should extract this block of code to a separate function; and then call that function on line 2495.5.

Code after:
          ;;
          --skip-default-packages)
            SKIP_DEFAULT_PACKAGES=true
          ;;
          *)
            ADDITIONAL_PARAMETERS="$ADDITIONAL_PARAMETERS $1"
          ;;
        esac
        shift
      done

      if [ -z "$SKIP_DEFAULT_PACKAGES" ] && [ -f "${NVM_DIR}/default-packages" ]; then
        DEFAULT_PACKAGES=""

        # Read lines from $NVM_DIR/default-packages
        local line
        while IFS=" " read -r line; do
          # Skip empty lines.
          [ -n "${line}" ] || continue

          # Skip comment lines that begin with `#`.
          [ "$(echo "$line" | cut -c1)" != "#" ] || continue

          # Fail on lines that have multiple space-separated words
          case ${line} in
            *\ * )
              nvm_err "Only one package per line is allowed in the ${NVM_DIR}/default-packages file. Please remove any lines with multiple space-seperated values."
              return 1
            ;;
          esac

          DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "
        done < "${NVM_DIR}/default-packages"
      fi

      if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then
        nvm_err "You can't reinstall global packages from the same version of node you're installing."
        return 4
      elif [ "${REINSTALL_PACKAGES_FROM-}" = 'N/A' ]; then
        nvm_err "If --reinstall-packages-from is provided, it must point to an installed version of node."
        return 5
      fi

      local FLAVOR
      if nvm_is_iojs_version "$VERSION"; then
        FLAVOR="$(nvm_iojs_prefix)"
      else
        FLAVOR="$(nvm_node_prefix)"
      fi

      if nvm_is_version_installed "$VERSION"; then