33

假设我有两个项目:

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

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

node 或 nvm 有没有办法在我cd进入每个项目时自动切换到所需的 node 版本?

4

8 回答 8

15

安装自动节点版本切换(avn)并添加.node-version指定您希望与项目一起使用的版本的文件。它通过已安装的版本管理器(例如nvmn.

于 2015-04-15T14:37:37.040 回答
12

每次. .nvmrc_ cd如果找到,它会通过加载版本nvm use并抛出任何输出。

cd() {
  builtin cd "$@"
  if [[ -f .nvmrc ]]; then
    nvm use > /dev/null
  fi
}
cd .
于 2018-08-26T18:20:57.000 回答
11

您可以将 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"
}
于 2015-04-15T17:22:51.620 回答
4

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

bash 脚本

自动调用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'

zsh 脚本

nvm use在带有.nvmrc文件的目录中自动调用每当您进入包含文件的目录时,
将其放入您的$HOME/.zshrcto中,该目录带有一个字符串,告诉 nvm 哪个节点:nvm use.nvmrcuse

# 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
于 2019-09-03T22:04:47.970 回答
4

NPM 现在让我们为这样的项目指定节点版本npm install node@8

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

于 2018-03-08T07:24:21.400 回答
0

如果您使用的是 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'
于 2018-07-14T14:45:25.057 回答
0

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

pip install nvshim  # this is all you need to do

它不会减慢您的 shell 启动速度,而是在您调用时移动查找哪个节点版本nodenpm或者npx通过填充这些二进制文件。文档中的更多详细信息。

来源,我写了这个工具。

于 2020-04-16T00:05:29.463 回答
0

如果您使用的是 ubuntu,请尝试以下操作,

 "scripts": {
  "prestart": ". ~/.nvm/nvm.sh && nvm use",
   ...
}

如果您已创建.nvmrc文件,则无需指定版本。

于 2021-08-24T11:45:18.640 回答