The exact file that will install the default version of node in a project using nvm?

In ruby, when using rbenv, you can make a .ruby-version file and put it in a local directory. https://gist.github.com/fnichol/1912050 Am I looking for something like this using NVM ?

Question:

Is there a property to install in package.json or a file to create that will install the default version of the project node?

+5
source share
3 answers

You can do this with a combination of NVM, dotfiles in the project directory and a small tool called direnv that allows you to load into environment variables based on each directory.

http://direnv.net/

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

Add to this directory a file named .nvmrc containing only the version number of the Node version that you want to automatically switch, for example:

 6.2.2 

Then add the environment configuration file named .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 exit cd from this directory, and then run cd , direnv will start and you will be asked to add the directory to your direnv whitelist by typing direnv allow . at the invitation. Once you have selected the whitelist, direnv will automatically run this script whenever you enter this directory, setting the version of Node to the version number in .nvmrc .

As a bonus, it will also add the node_modules directory to your PATH, so you can execute binary files from these directories without adding the node_modules path.

+6
source

There is currently built-in direnv stdlib support. It is documented in a wiki , but source is also easy to read, or type direnv stdlib to find it).

 $ 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 

If you want node_modules/.bin in your path, just add the layout node line to your .envrc (the source is also in direnv stdlib ).

+2
source

Here's how I do it, usually similar to Ross Shannon's answer, with a few differences:

  • You can specify the version of Node only in package.json , without requiring a .nvmrc file
  • You can also specify the version of Node directly in .envrc , again without the .nvmrc file
  • I am not adding node_modules/.bin to PATH, but if this is your preferred behavior, just add PATH_add node_modules/.bin to the use_nvm function.

For me, support for choosing Node for package.json is important, not .nvmrc , because I did not want to worry about synchronizing two files (especially in a project with several collaborators). However, this solution still works with .nvmrc .

This solution requires direnv , nvm and optionally (if you want to be able to select the Node version from package.json) jq .

In the ~/.config/direnv/direnvrc file, add the following:

 # 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 } 

In the project directory, add the .envrc file that calls use nvm , for example:

 use nvm package.json 

However, for my .envrc : I prefer something like the following:

 if declare -Ff use_nvm >/dev/null; then use nvm package.json fi 

for shared projects with shared .envrc , so collaborators who don’t have use nvm fail.

Now, when you enter the project directory, your desired version of Node will be automatically used (for the first time you will be asked to list your changes to .envrc using direnv allow ).

+1
source

Source: https://habr.com/ru/post/1235924/


All Articles