根据项目自动切换到正确的Node版本

时间:2015-04-15 14:30:50

标签: node.js nvm

假设我有两个项目:

example1: requires node version 0.12.1
example2: requires node version 0.10

目前,当我cd进入每个项目时,我会在运行应用程序之前使用nvm use <version>

对于node或nvm,是否有办法在cd进入每个项目时自动切换到所需的节点版本?

7 个答案:

答案 0 :(得分:7)

安装自动节点版本切换(avn)并添加.node-version文件,指定您要用于项目的版本。它会通过已安装的版本管理器自动检测并使用它,例如nvmn

答案 1 :(得分:7)

您可以将nvm命令添加到package.json文件

"scripts": {
  "preinstall": "nvm install 0.12.1",
  "prestart": "nvm use 0.12.1",
  "start": "node ./file1.js"
},

还将所需的版本设置为package.json,因此Continuous Integration服务会知道您要使用的版本。

{
  "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"
}

答案 2 :(得分:0)

如果您使用的是Bash Shell,则可以为cd定义一个Bash别名,当它检测到nvm install时,它将为您执行nvm use / .nvmrc文件。

alias cd='function cdnvm(){ cd $@; if [[ -f .nvmrc ]]; then <.nvmrc nvm install; fi; };cdnvm'

如果您要使cd离开目录时将Node版本恢复为默认版本,请使用以下别名:

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'

答案 3 :(得分:0)

每次.nvmrc时,都会在当前目录中查找cd文件。如果找到一个,它将通过nvm use加载版本并丢弃所有输出。

cd() {
  builtin cd "$@"
  if [[ -f .nvmrc ]]; then
    nvm use > /dev/null
  fi
}
cd .

答案 4 :(得分:0)

NVM GitHub README中还有扩展的(用户提供的)bash / zsh shell脚本:

bash脚本

  

自动调用nvm use
  该别名将从当前目录中搜索“ up”,以便检测到.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'

zsh脚本

  

在包含nvm use文件的目录中自动调用.nvmrc
  将其放入您的$HOME/.zshrc中,以便在您进入包含nvm use文件的目录时自动调用.nvmrc,该文件的字符串告诉nvm到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

答案 5 :(得分:0)

如果您可以使用其他工具,可以使用nvshim

pip install nvshim  # this is all you need to do

这不会减慢Shell的启动速度,而是通过填充二进制文件,从而在调用nodenpmnpx时将查找哪个节点版本的操作移至该位置。在docs中有更多详细信息。

来源,我写了这个工具。

答案 6 :(得分:-1)

NPM现在让你为这个npm install node@8项目指定节点版本。

因此,下次执行npm cinpm i时,会自动设置正确的版本。