mirror of
				https://github.com/nvm-sh/nvm.git
				synced 2025-11-03 22:47:13 +00:00 
			
		
		
		
	Speed up the zshrc hook reading .nvmrc
This commit is contained in:
		
							parent
							
								
									6cc90a4b8d
								
							
						
					
					
						commit
						a26573a157
					
				
							
								
								
									
										23
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								README.md
									
									
									
									
									
								
							@ -604,31 +604,24 @@ This alias would search 'up' from your current directory in order to detect a `.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
##### Calling `nvm use` automatically in a directory with a `.nvmrc` file
 | 
					##### Calling `nvm use` automatically in a directory with a `.nvmrc` file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Put this into your `$HOME/.zshrc` to call `nvm use` automatically whenever you enter a directory that contains an
 | 
					Put this into your `$HOME/.zshrc` after nvm initialization to call `nvm use` automatically whenever you enter a directory that contains an
 | 
				
			||||||
`.nvmrc` file with a string telling nvm which node to `use`:
 | 
					`.nvmrc` file with a string telling nvm which node to `use`:
 | 
				
			||||||
 | 
					 Usage: follow the [simple official guide to create a .nvmrc file](https://github.com/nvm-sh/nvm#nvmrc) containing the node version number in the directory you want to use.
 | 
				
			||||||
```zsh
 | 
					```zsh
 | 
				
			||||||
# place this after nvm initialization!
 | 
					# place this after nvm initialization!
 | 
				
			||||||
  autoload -U add-zsh-hook
 | 
					  autoload -U add-zsh-hook
 | 
				
			||||||
  load-nvmrc() {
 | 
					  load-nvmrc() {
 | 
				
			||||||
  local node_version="$(nvm version)"
 | 
					    local nvmrc_path=".nvmrc"
 | 
				
			||||||
  local nvmrc_path="$(nvm_find_nvmrc)"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if [ -n "$nvmrc_path" ]; then
 | 
					    if [ -f "$nvmrc_path" ]; then
 | 
				
			||||||
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
 | 
					      if [[ "$(which node)" != *"v$(cat "${nvmrc_path}")"* ]]; then
 | 
				
			||||||
 | 
					        nvm use --silent
 | 
				
			||||||
    if [ "$nvmrc_node_version" = "N/A" ]; then
 | 
					 | 
				
			||||||
      nvm install
 | 
					 | 
				
			||||||
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
 | 
					 | 
				
			||||||
      nvm use
 | 
					 | 
				
			||||||
      fi
 | 
					      fi
 | 
				
			||||||
  elif [ "$node_version" != "$(nvm version default)" ]; then
 | 
					    elif [[ "$(which node)" != *"v$(cat ~/.nvm/alias/default)"* ]]; then
 | 
				
			||||||
    echo "Reverting to nvm default version"
 | 
					      nvm use default --silent
 | 
				
			||||||
    nvm use default
 | 
					 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  add-zsh-hook chpwd load-nvmrc
 | 
					  add-zsh-hook chpwd load-nvmrc
 | 
				
			||||||
load-nvmrc
 | 
					 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### fish
 | 
					#### fish
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user