如何配置我的shell,使nvm use
在每次目录中有.nvmrc文件时自动运行,并在没有.nvmrc文件时使用最新版本或全局配置?
发布于 2014-05-09 12:13:59
我刚刚发现了Node.js https://github.com/wbyoung/avn的自动版本切换,你可以用它。
发布于 2020-05-30 05:49:00
还有另一个使用direnv的解决方案。Direnv自带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/*
这样的奇特结构。从积极的方面来看,direnv支持一系列的运行时,比如node、php、go、pyhton、ruby等等,允许我们使用单一的工具来解决路径问题。
发布于 2021-12-22 07:20:21
在使用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
https://stackoverflow.com/questions/23556330
复制相似问题