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

时间:2021-01-22 02:24:08

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 when there's no .nvmrc file?

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

5 个解决方案

#1


23  

I just found out about Automatic Version Switching for Node.js https://github.com/wbyoung/avn, you can use that.

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

You can also follow this thread https://github.com/creationix/nvm/issues/110

您也可以关注此主题https://github.com/creationix/nvm/issues/110

#2


16  

If you use zsh (z shell):

如果你使用zsh(z shell):

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

在具有.nvmrc文件的目录中自动调用'nvm use'

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:

将此放入$ 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" ] && [ "$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

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

#3


6  

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

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

enter_directory() {
  if [[ $PWD == $PREV_PWD ]]; then
    return
  fi

  PREV_PWD=$PWD
  [[ -f ".nvmrc" ]] && nvm use
}

export PROMPT_COMMAND=enter_directory

#4


3  

Excellent answer from @devius.

来自@devius的优秀答案。

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

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

~/.bashrc:

在〜/ .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

#5


0  

I'm not a fan of Gabo Esquivel's answer because I don't want to switch to another tool (avn) just to get this feature.

我不是Gabo Esquivel回答的粉丝,因为我不想转换到另一个工具(avn)来获得这个功能。

Instead, you can define a Bash alias to cd, which will do nvm install / nvm use for you when it detects a .nvmrc file., and revert back to the default when you cd out of the directory.

相反,您可以定义cd的Bash别名,当它检测到.nvmrc文件时,将为您执行nvm install / nvm。并在您退出目录时恢复为默认值。

alias cd='function cdnvm(){ cd $@; if [[ -f .nvmrc && -s .nvmrc  && -r .nvmrc ]]; then <.nvmrc nvm install; elif [[ $(nvm current) != $(nvm version default) ]]; then nvm use default; fi; };cdnvm'

See more variations of this at https://asciinema.org/a/191898

请访问https://asciinema.org/a/191898查看此更多信息

#1


23  

I just found out about Automatic Version Switching for Node.js https://github.com/wbyoung/avn, you can use that.

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

You can also follow this thread https://github.com/creationix/nvm/issues/110

您也可以关注此主题https://github.com/creationix/nvm/issues/110

#2


16  

If you use zsh (z shell):

如果你使用zsh(z shell):

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

在具有.nvmrc文件的目录中自动调用'nvm use'

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:

将此放入$ 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" ] && [ "$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

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

#3


6  

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

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

enter_directory() {
  if [[ $PWD == $PREV_PWD ]]; then
    return
  fi

  PREV_PWD=$PWD
  [[ -f ".nvmrc" ]] && nvm use
}

export PROMPT_COMMAND=enter_directory

#4


3  

Excellent answer from @devius.

来自@devius的优秀答案。

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

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

~/.bashrc:

在〜/ .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

#5


0  

I'm not a fan of Gabo Esquivel's answer because I don't want to switch to another tool (avn) just to get this feature.

我不是Gabo Esquivel回答的粉丝,因为我不想转换到另一个工具(avn)来获得这个功能。

Instead, you can define a Bash alias to cd, which will do nvm install / nvm use for you when it detects a .nvmrc file., and revert back to the default when you cd out of the directory.

相反,您可以定义cd的Bash别名,当它检测到.nvmrc文件时,将为您执行nvm install / nvm。并在您退出目录时恢复为默认值。

alias cd='function cdnvm(){ cd $@; if [[ -f .nvmrc && -s .nvmrc  && -r .nvmrc ]]; then <.nvmrc nvm install; elif [[ $(nvm current) != $(nvm version default) ]]; then nvm use default; fi; };cdnvm'

See more variations of this at https://asciinema.org/a/191898

请访问https://asciinema.org/a/191898查看此更多信息