【问题标题】:run `nvm use` automatically every time there's a .nvmrc file on the directory每次目录中有 .nvmrc 文件时自动运行 `nvm use`
【发布时间】:2014-06-26 16:54:51
【问题描述】:

如何配置我的shell,以便nvm use 每次目录中有.nvmrc 文件时自动运行,并在没有.nvmrc 文件时使用最新版本或全局配置?

【问题讨论】:

标签: javascript node.js shell nvm


【解决方案1】:

此版本将保留cd 的性能

autoload -U add-zsh-hook
use_nvmrc_version_automatically() { 
  if [[ -f .nvmrc ]]; then
    echo ".nvmrc FOUND now INSTALLING and USING $(cat .nvmrc)"
    nvm install $(cat .nvmrc) && nvm use $(cat .nvmrc)
  fi
}
add-zsh-hook chpwd use_nvmrc_version_automatically
use_nvmrc_version_automatically

【讨论】:

    【解决方案2】:

    当您使用fish shell 游泳时,以下是在目录中有.nvmrc 时运行nvm use 的方法:

    # TODO: save this as `$HOME/.config/fish/conf.d/use_nvmrc.fish` 
    
    # HOW IT WORKS
    # `nvm use` whenever .nvmrc is present in $PWD when using fish shell
    # when traveling deeper, use the parent .nvmrc unless otherwise set
    # also go back to default nvm when leaving the nvmrc-specified zone
    
    function set_nvm --on-event fish_prompt
        # runs whenever the fish_prompt event occurs
        # if the current directory hasn't changed, do nothing
        string match -q $PWD $PREV_PWD; and return 1
    
        # if the current directory is within the previous one where we found an nvmrc
        # and there is no subsequent .nvmrc here, do nothing, we are in the same repo
        string match -eq $PREV_PWD $PWD; and not test -e '.nvmrc'; and return 1
    
        # if we clear those checks, keep track of where we are
        set -g PREV_PWD $PWD
    
        if test -e '.nvmrc'
    
            # if we find .nvmrc, run nvm use
            nvm use
    
            # and remember that we used that node
            set NVM_DIRTY true
    
        else if not string match $NVM_DIRTY true
    
            # if we have set nvm and have stepped out of that repo
            # go back to default node, if not already on it
            not string match -eq (nvm current) (nvm alias default); and nvm use default
    
            # and clear the flag
            set NVM_DIRTY
        end
    end
    

    【讨论】:

      【解决方案3】:

      @devius 的出色回答。

      我只是对其进行了扩展,以便在将带有.nvmrc 的目录留给另一个没有它的目录时,它可以恢复为默认版本。

      ~/.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
      

      按照@doug-barbieri的建议,如果当前目录下没有.nvmrc文件但父子目录中有一个,下面的脚本不会将node改回默认版本目录。

      ~/.bashrc:

      enter_directory() {
        if [[ $PWD == $PREV_PWD ]]; then
          return
        fi
      
        if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; 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
      }
      

      诀窍就在这里:

      if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; then
        return
      fi
      

      它检查 PWD 是否包含 PREV_PWD。例如,如果/home/user1/a/b 包含/home/user1/a


      这可以扩展为与 Starship 一起使用(即使在 Windows 而非 WSL 的 Git Bash 上)也可以使用 starship_precmd_user_func

      set_win_title() {
        BASEPWD=$(basename "$PWD")
        echo -ne "\033]0; ? $BASEPWD \a" < /dev/null
        if [[ $PWD == $PREV_PWD ]]; then
          return
        fi
      
        if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; 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
      }
      starship_precmd_user_func="set_win_title"
      eval "$(starship init bash)"
      

      【讨论】:

      • 在 Windows 上使用 bash 会返回 node v.0.0 (64-bit) is not installed.,即使 .nvmrc 文件有 9.3
      • 使用VERSION=$(cat .nvmrc); nvm use $VERSION修复
      • 我还应该说这是在使用 nvm-for-windows,这是一个完全不同的野兽。但是通过上面的脚本+修改,它很有效。 +1
      • 这对我不起作用,因为如果我碰巧更改到当前项目的子目录,它会恢复为默认值。所以检测需要更聪明一点,在目录树中查找.nvmrc的第一个实例。
      【解决方案4】:

      对于那些使用 Apple Silicon (M1) Mac 的用户,您可能已经注意到 NVM 尝试并未能从 nvm install 的源代码编译 Node 版本 @Rotareti's and @Reynke's answer 的更新,它使用 Rosetta 为 Node = 16 安装本机 ARM 版本,因为 Node 16 是支持 Apple Silicon 的最早版本。

      安装后,您可以从本机或 Rosetta 终端 nvm use 使用正确的版本,因此只有原始功能的 nvm install 部分会发生变化。

      /opt/homebrew/opt/nvm/nvm.sh 替换为您的 nvm 安装路径。

      ~/.zshrc

      # 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
            # check if we're in a native (ARM) terminal
            if [[ $(uname -p) == "arm" ]]; then
              local nvmrc_remote_version=$(nvm version-remote "$(cat "${nvmrc_path}")")
      
              if printf '%s\n%s\n' v16.0.0 "${nvmrc_remote_version}" | sort -VC; then
                # arm and node >= v16; install native node
                nvm install
              else
                # arm and node < v16; install x64 node using rosetta
                arch -x86_64 zsh -c '. "/opt/homebrew/opt/nvm/nvm.sh"; nvm install'
                nvm use
              fi
            else
              # not arm
              nvm install
            fi
          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
      

      【讨论】:

        【解决方案5】:

        对于 Windows、zsh 和 coreybutler 的 nvm-windows 的用户,这个根据上述答案稍作修改的脚本可能会在您的 .zshrc 末尾派上用场:

        autoload -U add-zsh-hook
        load-nvmrc() {
          if [ -f ".nvmrc" ]; then
            local required_version=$(cat .nvmrc | cut -c2-)
            local current_version=$(node -v)
            echo "Required Node version: $required_version"
            local is_available_already=$(nvm ls | grep -c "$required_version")
            if [[ $required_version != $current_version && $is_available_already -lt 1 ]]; then
                echo "Required version $required_version not installed, installing..."
                nvm install $required_version
            fi
            nvm use $required_version
          fi
        }
        add-zsh-hook chpwd load-nvmrc
        load-nvmrc
        

        【讨论】:

          【解决方案6】:

          zsh 用户可以试试zsh-nvm:

          用于安装、更新和加载 nvm 的 Zsh 插件

          假设你正在使用抗原,你可以像这样打开自动使用:

          export NVM_AUTO_USE=true
          antigen bundle lukechilds/zsh-nvm
          

          zsh-nvm 还支持lazy load nvm,,大大减少了 zsh 的启动时间

          % time (source "$NVM_DIR/nvm.sh")
          ( source "$NVM_DIR/nvm.sh"; )  0.58s user 0.37s system 109% cpu 0.874 total
          
          % time (_zsh_nvm_lazy_load)
          ( _zsh_nvm_lazy_load; )  0.01s user 0.01s system 168% cpu 0.012 total
          

          【讨论】:

            【解决方案7】:

            如果你使用zsh(z shell):

            我以更快的不同方式加载 nvm,但这意味着 nvm_find_nvmrc 不可用,因此 @Rotareti 解决方案不适合我。

            我找到了任何简单的解决方法:只需不带参数调用nvm use,因为它已经处理了查找.nvmrc 文件本身的逻辑,如果找不到则使用默认版本。

            # ~/.zshrc
            
            # DEFAULT NVM CONFIG
            #export NVM_DIR="$HOME/.nvm"
            #[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
            #[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
            
            # Add the following lines before all other OH MY ZSH config
            # FASTER WAY TO CONFIGURE NVM ON STARTUP - OTHERWISE IT'S REALLY SLOW
            export NVM_DIR="$HOME/.nvm"
            [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
            export PATH="$NVM_DIR/versions/node/v$(<$NVM_DIR/alias/default)/bin:$PATH"
            alias nvm="unalias nvm; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; nvm $@"
            
            # USE NVM VERSION IF .NVMRC FOUND, OTHERWISE USE DEFAULT
            nvm use &>/dev/null
            
            

            总而言之,我发现这是一个非常快速的解决方案,它省去了我输入nvm use 的痛苦。

            当没有可用的.nvmrc 文件时,我想避免使用nvm use,但是我的计算机上的加载时间非常短,而且我很少需要没有Node 的终端——所以这对我现在有用。

            更新:添加了关于放置建议脚本的位置的说明

            【讨论】:

            • 在 Big Sur 上更改文件夹时不会更新
            • @MildFuzz 你的意思是当你从不同的位置打开一个新的终端时?如果这就是您的意思,那么您需要在 zsh 配置的所有其余部分之前将建议的脚本添加到您的 .zshrc 文件中。
            【解决方案8】:

            Bash 版本(放入 $HOME/.bashrc),具有以下功能:

            • 不使用cd 别名(它允许以其他方式更改目录,例如直接在另一个目录中启动终端)
            • 在上层目录中找到.nvmrc (using nvm_find_nvmrc)
            • 如果不需要,不会打电话给nvm use
            call_nvm_use_if_needed() {
              NEW_NVMRC="$(nvm_find_nvmrc)"
              if [[ "$NEW_NVMRC" != "$CURRENT_NVMRC" ]]; then
                if [[ -z "$NEW_NVMRC" ]]; then
                  nvm use default
                else
                  nvm use
                fi
                CURRENT_NVMRC="$NEW_NVMRC"
              fi
            }
            
            PROMPT_COMMAND="call_nvm_use_if_needed; ${PROMPT_COMMAND}"
            

            【讨论】:

            • osx big sur 上的 CD 后似乎不尊重新文件夹
            【解决方案9】:

            使用direnv 的另一个解决方案。 Direenv 带有 OS X 和许多发行版,因此无需安装。

            将这两行添加到您的 .zshenv 或 .bash_profile 中,具体取决于您使用的 shell:

            
            export NVM_DIR="$HOME/.nvm" # You probably have this line already
            export NODE_VERSIONS="${NVM_DIR}/versions/node"
            export NODE_VERSION_PREFIX="v"
            

            将 .envrc 文件添加到项目根目录中

            set -e
            use node
            

            最后 cd 到你的目录。 (别忘了来源 .zshenv)

            direnv 会要求您允许加载配置。 输入direnv allow,瞧!

            请注意,direnv 不支持像 .nvrmc 中的 lts/* 这样的花哨结构。从积极的方面来说,direnv 支持一系列运行时,如 node、php、go、pyhton、ruby 等,允许我们使用单个工具来解决路径问题。

            【讨论】:

            • 谢谢,但我不认为 direnv 默认包含在 MacOS 上
            【解决方案10】:

            我为此尝试了很多解决方案,但没有任何一种方法能达到我想要的效果,所以我自己写了:

            ZSH function to auto-switch to correct Node version

            据我所知,这是唯一符合以下所有条件的:

            • 通过在目录树中搜索最接近的.nvmrc(就像nvm use),确保您始终使用正确的版本;
            • 可以处理任何有效的.nvmrc 格式;
            • 如果没有安装的版本满足.nvmrc,会明确警告您,
            • 假设你想要default,如果树上的任何地方都没有.nvmrc
            • 如果您已经在正确的 Node 版本上,完全安静且快速

            【讨论】:

              【解决方案11】:

              如果你使用 zsh (z shell):

              在带有 .nvmrc 文件的目录中自动调用“nvm use”

              把它放到你的 $HOME/.zshrc 中,当你进入一个包含 .nvmrc 文件的目录时,它会自动调用 nvm 使用,其中包含一个告诉 nvm 使用哪个节点的字符串:

              # 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
              

              更多信息:https://github.com/creationix/nvm#zsh

              【讨论】:

              • 这对我来说效果很好,但对cd 造成了性能损失,因为它会在您每次更改目录时运行。我在 load-nvmrc() 的第一行添加了[[ -a .nvmrc ]] || return,这显着提高了它的性能
              • @Belgabad 它的性能更高,因为在您的行中,脚本不会检查父目录中的.nvmrc,并且当您使用@987654327 时,它也不会返回到默认的node 版本@退出项目目录。
              • @Belgabad 你在哪里添加那行?在local 行之前?
              • 如果你使用Powerlevel10k,别忘了在nvm use后面加上--silent,以避免zsh初始化时控制台输出警告
              【解决方案12】:

              此答案取自official nvm documentation

              将以下内容放在$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'
              

              这是对以下方面的改进:

              此别名将从您的当前目录中搜索“向上”以检测.nvmrc 文件。如果找到它,它将切换到该版本;如果没有,它将使用默认版本。

              【讨论】:

              • 看起来有点慢,但我真的很喜欢这个选项
              • @BossmanT “看起来有点慢”,你的意思是说你会从它的作用中猜测它很慢,或者你实际上发现它运行很慢?
              • @callum 只是观察它在我的 bash 环境中运行,但没有对其进行时间试验,它慢了大约几分之一秒,当然没有任何意义
              • 我没有发现任何延迟。运行cd 是不可能大批量运行的,所以我认为这并不重要,除非延迟对人类来说很明显。
              • 谢谢@d4nyll,这真的很有用,已添加到我的点文件中
              【解决方案13】:

              对于仍然面临上述问题的人,nvm 的 README 包含此部分,这将有所帮助 https://github.com/creationix/nvm#deeper-shell-integration

              我个人更喜欢编辑.bashrc (https://github.com/creationix/nvm#automatically-call-nvm-use) 而不是其他解决方案。

              【讨论】:

                【解决方案14】:

                我使用这个名为 Oh My Zsh 的 zsh 配置框架。这是一个非常活跃的存储库,定期更新。试试吧,我相信你会喜欢的。哦,它内置了自动 .nvmrc 功能,所以它就像通过 npm 安装包一样简单!

                https://github.com/robbyrussell/oh-my-zsh

                【讨论】:

                • 我不认为这个功能是内置的
                • 这几天居然有:如果NVM_AUTOLOAD设置为1,插件会在当前工作目录中找到.nvmrc文件时自动加载节点版本,指示哪个节点要加载的版本。见:github.com/ohmyzsh/ohmyzsh/tree/master/plugins/nvm
                【解决方案15】:

                @Adriano P 答案上扩展,我建议这个不太通用的版本(仅当.nvmrc 设置在 git 存储库根目录时才有效),但在我们导航的情况下有效到项目的其他地方而不是它的根:

                _enter_dir() {
                    local git_root
                    git_root=$(git rev-parse --show-toplevel 2>/dev/null)
                
                    if [[ "$git_root" == "$PREV_PWD" ]]; then
                        return
                    elif [[ -n "$git_root" && -f "$git_root/.nvmrc" ]]; then
                        nvm use
                        NVM_DIRTY=1
                    elif [[ "$NVM_DIRTY" == 1 ]]; then
                        nvm use default
                        NVM_DIRTY=0
                    fi
                    PREV_PWD="$git_root"
                }
                
                export PROMPT_COMMAND=_enter_dir
                #export PROMPT_COMMAND="$PROMPT_COMMAND;_enter_dir"  # use this if PROMPT_COMMAND already defined
                

                【讨论】:

                  【解决方案16】:

                  如果您使用 bash,您可以将其添加到您的 ~/.bashrc 文件中:

                  enter_directory() {
                    if [[ $PWD == $PREV_PWD ]]; then
                      return
                    fi
                  
                    PREV_PWD=$PWD
                    [[ -f ".nvmrc" ]] && nvm use
                  }
                  
                  export PROMPT_COMMAND=enter_directory
                  

                  【讨论】:

                  • 这是我的首选方法。非常感谢您发布这个!
                  【解决方案17】:

                  我刚刚发现了 Node.js https://github.com/wbyoung/avn 的自动版本切换,你可以使用它。

                  你也可以关注这个话题 https://github.com/creationix/nvm/issues/110

                  【讨论】:

                  • 这令人沮丧..工具堆栈深度没有限制吗? node → npm → nvm → avn,然后备份。 sigh 我会打球,但我的热情随着每一次 hack 而减弱。 (当然,感谢您的链接,加博。)
                  • 上述线程中的所有 cmets 都没有为我使用 bash。也许它们都适用于 MacOS。对于适用于 Ubuntu 的内容,请查看下面的答案。
                  猜你喜欢
                  • 1970-01-01
                  • 2016-03-21
                  • 1970-01-01
                  • 2019-12-24
                  • 1970-01-01
                  • 2019-04-27
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多