run `nvm use` automatically every time there's a .nvmrc file on the directory

前端 未结 10 1746
南笙
南笙 2020-12-23 11:05

How to configure my shell so that nvm use run automatically every time there\'s a .nvmrc file on the directory and use the latest version or a global config whe

相关标签:
10条回答
  • 2020-12-23 11:37

    If you use bash you can add this to your ~/.bashrc file:

    enter_directory() {
      if [[ $PWD == $PREV_PWD ]]; then
        return
      fi
    
      PREV_PWD=$PWD
      [[ -f ".nvmrc" ]] && nvm use
    }
    
    export PROMPT_COMMAND=enter_directory
    
    0 讨论(0)
  • 2020-12-23 11:39

    If you use zsh (z shell):

    Calling 'nvm use' automatically in a directory with a .nvmrc file

    Put this into your $HOME/.zshrc to call nvm use automatically whenever you enter a directory that contains an .nvmrc file with a string telling nvm which node to use:

    # place this after nvm initialization!
    autoload -U add-zsh-hook
    load-nvmrc() {
      local node_version="$(nvm version)"
      local nvmrc_path="$(nvm_find_nvmrc)"
    
      if [ -n "$nvmrc_path" ]; then
        local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
    
        if [ "$nvmrc_node_version" = "N/A" ]; then
          nvm install
        elif [ "$nvmrc_node_version" != "$node_version" ]; then
          nvm use
        fi
      elif [ "$node_version" != "$(nvm version default)" ]; then
        echo "Reverting to nvm default version"
        nvm use default
      fi
    }
    add-zsh-hook chpwd load-nvmrc
    load-nvmrc
    

    More info: https://github.com/creationix/nvm#zsh

    0 讨论(0)
  • 2020-12-23 11:41

    Excellent answer from @devius.

    I just extended it so it can revert to the default version when leaving a directory with .nvmrc to another without it.

    ~/.bashrc:

    #
    # Run 'nvm use' automatically every time there's 
    # a .nvmrc file in the directory. Also, revert to default 
    # version when entering a directory without .nvmrc
    #
    enter_directory() {
    if [[ $PWD == $PREV_PWD ]]; then
        return
    fi
    
    PREV_PWD=$PWD
    if [[ -f ".nvmrc" ]]; then
        nvm use
        NVM_DIRTY=true
    elif [[ $NVM_DIRTY = true ]]; then
        nvm use default
        NVM_DIRTY=false
    fi
    }
    
    export PROMPT_COMMAND=enter_directory
    
    0 讨论(0)
  • 2020-12-23 11:44

    This answer is taken from the official nvm documentation.

    Put the following at the end of your $HOME/.bashrc:

    find-up () {
        path=$(pwd)
        while [[ "$path" != "" && ! -e "$path/$1" ]]; do
            path=${path%/*}
        done
        echo "$path"
    }
    
    cdnvm(){
        cd "$@";
        nvm_path=$(find-up .nvmrc | tr -d '[:space:]')
    
        # If there are no .nvmrc file, use the default nvm version
        if [[ ! $nvm_path = *[^[:space:]]* ]]; then
    
            declare default_version;
            default_version=$(nvm version default);
    
            # If there is no default version, set it to `node`
            # This will use the latest version on your machine
            if [[ $default_version == "N/A" ]]; then
                nvm alias default node;
                default_version=$(nvm version default);
            fi
    
            # If the current version is not the default version, set it to use the default version
            if [[ $(nvm current) != "$default_version" ]]; then
                nvm use default;
            fi
    
            elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
            declare nvm_version
            nvm_version=$(<"$nvm_path"/.nvmrc)
    
            # Add the `v` suffix if it does not exists in the .nvmrc file
            if [[ $nvm_version != v* ]]; then
                nvm_version="v""$nvm_version"
            fi
    
            # If it is not already installed, install it
            if [[ $(nvm ls "$nvm_version" | tr -d '[:space:]') == "N/A" ]]; then
                nvm install "$nvm_version";
            fi
    
            if [[ $(nvm current) != "$nvm_version" ]]; then
                nvm use "$nvm_version";
            fi
        fi
    }
    alias cd='cdnvm'
    

    This is an improvement over:

    • @Gabo Esquivel's answer - because you won't have to switch to another tool (avn)
    • @devius's and @Adriano P's answers - which do not deal with situations where you are within a project

    This alias would search 'up' from your current directory in order to detect a .nvmrc file. If it finds it, it will switch to that version; if not, it will use the default version.

    0 讨论(0)
提交回复
热议问题