繁体   English   中英

每次目录中有 .nvmrc 文件时自动运行 `nvm use`

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

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

如果您使用zsh (z shell):

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

将其放入您的 $HOME/.zshrc 以在您进入包含 .nvmrc 文件的目录时自动调用 nvm use ,该文件带有一个告诉 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

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

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

if ! [[ "${PROMPT_COMMAND:-}" =~ _nvmrc_hook ]]; then
  PROMPT_COMMAND="_nvmrc_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi

@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文件但父子目录中有一个文件,则下面的脚本不会将节点更改回默认版本。

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

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

npm install -g avn avn-nvm avn-n
avn setup

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

此答案取自官方 nvm 文档

将以下内容放在$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文件。 如果找到它,它将切换到该版本; 如果没有,它将使用默认版本。

我为此尝试了许多解决方案,但都没有达到我想要的效果,所以我写了自己的:

ZSH 功能自动切换到正确的节点版本

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

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

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

根据您使用的 shell,将这两行添加到您的 .zshenv 或 .bash_profile 中:


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 lts/*这样的花哨结构。 从积极的方面来说,direnv 支持一系列运行时,如 node、php、go、pyhton、ruby 等,允许我们使用单个工具来解决路径问题。

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

  • 不使用cd别名(它允许以其他方式更改目录,例如直接在另一个目录中启动终端)
  • 在上层目录中查找.nvmrcusing 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}"

当您使用.nvmrc fish以下是如何运行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

现在有关于如何做到这一点的官方文档,请参阅https://github.com/nvm-sh/nvm#deeper-shell-integration上的 deep-shell-integration

如果您使用 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 nvm,但是我的计算机上的加载时间非常短,而且我很少需要没有 Node 的终端 - 所以这对我现在有用。

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

对于 zsh 用户,您应该尝试zsh-nvm

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

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

export NVM_AUTO_USE=true
antigen bundle lukechilds/zsh-nvm

zsh-nvm 还支持延迟加载 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

对于 Apple Silicon (M1) Mac 上的用户,您可能已经注意到 NVM 尝试从nvm install上的源代码编译节点版本 <16 并且失败。 这是@Rotareti 和@Reynke 的答案的更新,它使用 Rosetta 为 Node < 16 安装 x86_64 版本,同时为 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

此版本将保留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

扩展@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

对于仍然面临上述问题的人, nvm的自述文件有这一部分,这将很有帮助https://github.com/creationix/nvm#deeper-shell-integration

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

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

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

对于 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

我实际上已经使用了 Rotareti 的解决方案一段时间,如果没有.nvmrc (这实际上是作者最初要求的),我想使用 lts版本

所以我做了一点工作,只需要一些替换/添加的行:

# Load right version of NVM
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-remote --lts)" ]; then
    if [[ "$(nvm ls)" != *"$(nvm version-remote --lts)"* ]]; then
      echo "New lts version available. Installing..."
      nvm install --lts
    fi
    echo "Reverting to node lts version"
    nvm use --lts
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM