假设我有两个项目:
example1: requires node version 0.12.1
example2: requires node version 0.10
目前,当我cd
进入每个项目时,我nvm use <version>
在运行应用程序之前使用。
node 或 nvm 有没有办法在我cd
进入每个项目时自动切换到所需的 node 版本?
安装自动节点版本切换(avn)并添加.node-version
指定您希望与项目一起使用的版本的文件。它通过已安装的版本管理器(例如nvm
和n
.
每次. .nvmrc
_ cd
如果找到,它会通过加载版本nvm use
并抛出任何输出。
cd() {
builtin cd "$@"
if [[ -f .nvmrc ]]; then
nvm use > /dev/null
fi
}
cd .
您可以将 nvm 命令添加到 package.json 文件中
"scripts": {
"preinstall": "nvm install 0.12.1",
"prestart": "nvm use 0.12.1",
"start": "node ./file1.js"
},
还要在 package.json 中设置所需的版本,这样持续集成服务就会知道你想使用什么版本。
{
"name": "naive",
"description": "A package using naive versioning",
"author": "A confused individual <iam@confused.com>",
"dependencies": {
"express": ">= 1.2.0",
"optimist": ">= 0.1.0"
},
"engine": "node 0.4.1"
}
NVM GitHub README中还有扩展的(用户贡献的)bash/zsh shell 脚本:
自动调用
nvm use
此别名将从您的当前目录中搜索“向上”以检测.nvmrc
文件。如果找到它,它将切换到该版本;如果没有,它将使用默认版本。将以下内容放在您的末尾
$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)
declare locally_resolved_nvm_version
# `nvm ls` will check all locally-available versions
# If there are multiple matching versions, take the latest one
# Remove the `->` and `*` characters and spaces
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')
# If it is not already installed, install it
# `nvm install` will implicitly use the newly-installed version
if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
nvm install "$nvm_version";
elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
nvm use "$nvm_version";
fi
fi
}
alias cd='cdnvm'
nvm use
在带有.nvmrc
文件的目录中自动调用每当您进入包含文件的目录时,
将其放入您的$HOME/.zshrc
to中,该目录带有一个字符串,告诉 nvm 哪个节点:nvm use
.nvmrc
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
NPM 现在让我们为这样的项目指定节点版本npm install node@8
。
因此,下次您执行npm ci
ornpm i
时,会自动设置正确的版本。
如果您使用的是 Bash shell,您可以为 定义一个 Bash 别名cd
,它会在检测到文件时为您执行nvm install
/ 。nvm use
.nvmrc
alias cd='function cdnvm(){ cd $@; if [[ -f .nvmrc ]]; then <.nvmrc nvm install; fi; };cdnvm'
如果要在离开目录时使 Node 版本恢复为默认版本cd
,请使用以下别名:
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'
如果您使用的是 ubuntu,请尝试以下操作,
"scripts": {
"prestart": ". ~/.nvm/nvm.sh && nvm use",
...
}
如果您已创建.nvmrc
文件,则无需指定版本。