问题
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?
回答1:
If you use 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
More info: https://github.com/creationix/nvm#zsh
回答2:
If you use bash you can add this to your ~/.bashrc
file:
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
[[ -f ".nvmrc" ]] && nvm use
}
export PROMPT_COMMAND=enter_directory
回答3:
I just found out about Automatic Version Switching for Node.js https://github.com/wbyoung/avn, you can use that.
You can also follow this thread https://github.com/creationix/nvm/issues/110
回答4:
Excellent answer from @devius.
I just extended it so it can revert to the default version when leaving a directory with .nvmrc
to another without it.
~/.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:
This answer is taken from the official nvm documentation.
Put the following at the end of your $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'
This is an improvement over:
- @Gabo Esquivel's answer - because you won't have to switch to another tool (avn)
- @devius's and @Adriano P's answers - which do not deal with situations where you are within a project
This alias would search 'up' from your current directory in order to detect a .nvmrc
file. If it finds it, it will switch to that version; if not, it will use the default version.
回答6:
Extending on @Adriano P answer, I'd propose this version that is less general (only works if .nvmrc
is set on a git repository root), but works in cases when we navigate to elsewhere in project than its root:
_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
回答7:
I use this zsh configuration framework called Oh My Zsh. It's a very active repository with regular updates. Try it and I'm sure you will love it. Oh, and it has the automatic .nvmrc feature built-in so it's as simple as installing the package thru npm!
https://github.com/robbyrussell/oh-my-zsh
回答8:
For someone still facing the above issue the README for nvm
has this section which would be helpful
https://github.com/creationix/nvm#deeper-shell-integration
Personally I prefer editing the .bashrc
(https://github.com/creationix/nvm#automatically-call-nvm-use) over other solutions.
回答9:
I tried many solutions for this and nothing worked the way I wanted, so I wrote my own:
ZSH function to auto-switch to correct Node version
As far as I know, this is the only one that meets all the following criteria:
- guarantees you are always on the right version by searching up the directory tree to find the closest
.nvmrc
(just likenvm use
); - can handle any valid
.nvmrc
format; - clearly warns you if no installed version satisfies the
.nvmrc
, - assumes you want
default
if there is no.nvmrc
anywhere up the tree; - is completely silent and fast if you are already on the correct Node version.
来源:https://stackoverflow.com/questions/23556330/run-nvm-use-automatically-every-time-theres-a-nvmrc-file-on-the-directory