一个dotfile,它将使用nvm在项目上设置默认节点版本?

时间:2015-11-13 20:48:55

标签: javascript node.js version-control nvm

在ruby中使用rbenv时,您可以创建一个.ruby-version文件并将其放在本地目录中。 https://gist.github.com/fnichol/1912050 我正在使用NVM寻找与此类似的内容?

问题:

是否有要在package.json中设置的属性或要创建的文件,它将设置项目节点的默认版本?

4 个答案:

答案 0 :(得分:11)

You can do this with a combination of NVM, dotfiles in your project directory, and a little tool called direnv which allows you to load in environment variables on a per-directory basis.

http://direnv.net/

Install NVM and direnv, and then cd to the directory you want to change Node versions in.

Add a file called .nvmrc in that directory, containing just the version number of the Node version you want to auto-switch to, e.g.,:

6.2.2

Then add an environment configuration file called .envrc to your directory, containing this script:

nvmrc=~/.nvm/nvm.sh
if [ -e $nvmrc ]; then
  source $nvmrc
  nvm use
fi

PATH_add node_modules/.bin

If you now cd out of this directory, and then cd back in, direnv will kick in and you should be asked to add the directory to your direnv whitelist by typing direnv allow . at the prompt. Once whitelisted, direnv will auto-run that script whenever you enter this directory, setting your Node version to the version number in .nvmrc.

As a bonus, it will also add the node_modules directory to your PATH, so you can execute binaries from those directories without prepending the node_modules path.

答案 1 :(得分:3)

现在有一些本机支持这种内置于direnv的stdlib中。它已在wiki中记录,但source同样易于阅读,或键入direnv stdlib即可查找。)

$ node -v
v8.0.0
$ cat .node-version
4.3.2
$ cat .envrc
use node
$ export NODE_VERSIONS=~/.nvm/versions/node
$ export NODE_VERSION_PREFIX=v
$ direnv allow
direnv: loading ~/.config/direnv/direnvrc
direnv: loading .envrc
direnv: using node
direnv: Successfully loaded NodeJS v4.3.2 (via .node-version), from prefix (~/.nvm/versions/node/v4.3.2)
direnv: export +CPATH +LD_LIBRARY_PATH +LIBRARY_PATH +PKG_CONFIG_PATH ~MANPATH ~PATH
$ node -v
v4.3.2
$ direnv deny
direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
$ node -v
v8.0.0

如果您希望路径中显示node_modules/.bin,只需将layout node行添加到.envrcsource也在direnv stdlib输出中)。< / p>

答案 2 :(得分:2)

以下是我的表现方式,一般与Ross Shannon的答案相似,但有一些不同之处:

  1. 您可以只在package.json中指定节点版本,而无需.nvmrc文件
  2. 您还可以直接在.envrc中指定节点版本,同样没有.nvmrc文件
  3. 我没有将node_modules/.bin添加到PATH,但如果这是您的首选行为,只需将PATH_add node_modules/.bin添加到use_nvm功能。
  4. 对我来说,支持从package.json而不是.nvmrc选择节点版本非常重要,因为我不想担心保持两个文件同步(特别是在有多个协作者的项目中) 。也就是说,此解决方案仍适用于.nvmrc

    此解决方案需要direnvnvm并且可选(如果您希望能够从package.json中选择节点版本)jq

    ~/.config/direnv/direnvrc文件中,添加以下内容:

    # To use:
    # 1) Node version specified in package.json, in .envrc add:
    #      use nvm package.json
    #    This requires that package.json contains something like
    #      "engines": {
    #        "node": ">=6.9.2"
    #      },
    #
    # 2) Node version specified in .envrc add:
    #      use nvm 6.9.2
    #
    # 3) Node version specified in .nvmrc, in .envrc add:
    #      use nvm
    use_nvm() {
      local node_version=$1
    
      if [[ $node_version = "package.json" ]]; then
        if has jq; then
          node_version=$(jq --raw-output .engines.node package.json | tr -d "<=> ")
        else
          echo "Parsing package.json for node version to use with direnv requires jq"
        fi
      fi
    
      nvm_sh=~/.nvm/nvm.sh
      if [[ -e $nvm_sh ]]; then
        source $nvm_sh
        nvm use $node_version
      fi
    }
    

    在项目目录中,添加一个调用.envrc的{​​{1}}文件,例如:

    use nvm

    但是,对于我的use nvm package.json

    ,我更喜欢以下内容
    .envrc

    对于共享if declare -Ff use_nvm >/dev/null; then use nvm package.json fi 的共享项目,以便没有.envrc定义的协作者不会收到错误。

    现在,当您输入项目目录时,将自动使用您想要的节点版本(第一次,系统会提示您将对use nvm的更改列入白名单.envrc

答案 3 :(得分:0)

注意:此解决方案不会自动切换文件夹版本。

其他答案对我没有帮助,因此,这是我遵循的解决方案:

创建一个.nvmrc文件并指定您希望应用程序在其上运行的节点版本。

touch .nvmrc

打开文件并指定版本,例如13.3.0

.nvmrc中定义版本后,也可以在engines文件中定义package.json,以确保该版本符合要求(如果不符合要求) ,运行npm install将会失败。

"engineStrict": true,
"engines": {
  "node": "13.3.0"
}

以下是如果它与节点版本不匹配将遇到的错误。

Source: My Project

最后,要确保它切换到正确的节点版本,您可以在导航到该目录后运行以下命令。

nvm use

这将切换到所需的版本,或者您可以将其添加到script中的package.json命令之一,例如:

"scripts": { 
  /*
   * here, the second command (nodemon server.js) will change base on the dev 
   * server you are using and the path where you've server.js
  */
  "dev:app:run": "nvm use; nodemon server.js"
},

以上内容将在启动服务器之前切换节点版本。