Automatically switch to the correct version of Node based on the project

Let's say I have 2 projects:

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

Currently, when I cd into each project, I use nvm use <version> before running the application.

Is there a way with node or nvm to automatically switch to the correct version of node when I cd into each project?

+10
source share
6 answers

Install Automatic Node Version Switching ( avn ) and add a .node-version file that indicates the version you want to use with the project. It automatically detects and uses it through an installed version manager, such as nvm and n .

+8
source

You can add nvm command to package.json file

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

Also install the desired version in package.json, so continuous integration services will know which version you want to use.

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

NPM now allows you to specify the node version for a project such as npm install node@8 .

So, the next time you run npm ci or npm i , the correct version will be automatically installed.

+1
source

If you use a Bash shell, you can define a Bash alias for cd that nvm install / nvm use will do for you when it detects a .nvmrc file.

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

If you want the Node version to revert to the original default when you cd out of the directory, use the following alias:

 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' 
0
source

.nvmrc for the .nvmrc file in your current directory, every time you cd . If it is found, it downloads the version via nvm use and throws out any output.

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

The NVM GitHub README also has extended (user-provided) bash / zsh shell scripts:

bash script

Automatic call nvm use
This alias will look up from your current directory to find the .nvmrc file. If he finds him, he will switch to this version; if not, the default version will be used.

Put the following at the end of your $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 script

Call nvm use automatically in the directory with the .nvmrc file
Put this in your $HOME/.zshrc to automatically call nvm use whenever you enter a directory containing the .nvmrc file with a line telling nvm which node to 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 
0
source

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


All Articles