gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:54:07 33 4
gpt4 key购买 nike

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

最佳答案

如果你使用 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

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

关于javascript - 每次目录中有 .nvmrc 文件时自动运行 `nvm use`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556330/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com