【问题标题】:Automatically switch to correct version of Node based on project根据项目自动切换到正确的Node版本
【发布时间】:2015-06-21 13:54:51
【问题描述】:

假设我有 2 个项目:

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

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

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

【问题讨论】:

  • 怀疑。但是,您可以运行一个 npm 来执行此操作。
  • 您可以设置一个 bash 脚本并从那里调用。合理的

标签: node.js nvm


【解决方案1】:

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

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

如果你已经创建了.nvmrc 文件,那么你不需要指定版本。

【讨论】:

    【解决方案2】:

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

    pip install nvshim  # this is all you need to do
    

    它不会减慢您的 shell 启动速度,而是在您调用 nodenpmnpx 时通过填充这些二进制文件来查找哪个节点版本。更多详情请关注docs

    来源,我写了这个工具。

    【讨论】:

      【解决方案3】:

      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 脚本

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

      【讨论】:

        【解决方案4】:

        每次cd 时,都会在当前目录中查找.nvmrc 文件。如果找到,它会通过nvm use 加载版本并抛出任何输出。

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

        【讨论】:

        • 简单易用,最棒的。谢谢
        【解决方案5】:

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

        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'
        

        【讨论】:

          【解决方案6】:

          现在,NPM 让您为类似 npm install node@8 的项目指定节点版本。

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

          【讨论】:

            【解决方案7】:

            您可以将 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"
            }
            

            【讨论】:

            • 当我运行npm install 时,它无法运行第一个命令并显示nvm command not foundnvm 已安装,我可以从 shell 运行命令。
            • 我遇到了@ajay 上面报告的同样问题
            • @ajay 你需要先执行nvm.sh文件。示例sh ~/.nvm/nvm.sh &amp;&amp; nvm use 14.15.4.
            • 这对我不起作用。连老德的建议都没有。我正在使用 .zshrc 和 macosx,但需要它是跨平台的。控制台打印如下:&gt; sh ~/.nvm/nvm.sh &amp;&amp; nvm use 12.18.3 sh: nvm: command not found
            【解决方案8】:

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

            【讨论】:

            • 该项目的所有者不再维护 avn,因此该解决方案将无法在 Ubuntu 上运行;尝试设置时会出现错误。
            猜你喜欢
            • 2021-10-12
            • 1970-01-01
            • 1970-01-01
            • 2021-10-07
            • 2012-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多