Diff
1diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md
2index ab71185cba0193963fd36514cc07bfd93b468268..749a43403e3db9a16e8a8b72bf6ac392fb360866 100644
3--- a/plugins/nvm/README.md
4+++ b/plugins/nvm/README.md
5@@ -20,3 +20,7 @@ plugins=(... nvm)
6 - **`NVM_LAZY`**: if you want the plugin to defer the load of nvm to speed-up the start of your zsh session,
7 set `NVM_LAZY` to `1`. This will use the `--no-use` parameter when loading nvm, and will create a function
8 for `node`, `npm` and `yarn`, so when you call either of these three, nvm will load with `nvm use default`.
9+
10+- **`NVM_AUTOLOAD`**: if `NVM_AUTOLOAD` is set to `1`, the plugin will automatically load a node version when
11+ if finds a [`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating
12+ which node version to load.
13diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
14index b2355b7eff79af188e98e1cb14092a64ff740615..1e9b26e7a9232837655c857895bedcf0d0c54555 100644
15--- a/plugins/nvm/nvm.plugin.zsh
16+++ b/plugins/nvm/nvm.plugin.zsh
17@@ -35,6 +35,33 @@ if (( $+NVM_LAZY )); then
18 }
19 fi
20
21+# Autoload nvm when finding a .nvmrc file in the current directory
22+# Adapted from: https://github.com/nvm-sh/nvm#zsh
23+if (( $+NVM_AUTOLOAD )); then
24+ load-nvmrc() {
25+ local node_version="$(nvm version)"
26+ local nvmrc_path="$(nvm_find_nvmrc)"
27+
28+ if [[ -n "$nvmrc_path" ]]; then
29+ local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
30+
31+ if [[ "$nvmrc_node_version" = "N/A" ]]; then
32+ nvm install
33+ elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
34+ nvm use
35+ fi
36+ elif [[ "$node_version" != "$(nvm version default)" ]]; then
37+ echo "Reverting to nvm default version"
38+ nvm use default
39+ fi
40+ }
41+
42+ autoload -U add-zsh-hook
43+ add-zsh-hook chpwd load-nvmrc
44+
45+ load-nvmrc
46+fi
47+
48 # Load nvm bash completion
49 for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
50 if [[ -f "$nvm_completion" ]]; then
51@@ -47,4 +74,4 @@ for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_complet
52 fi
53 done
54
55-unset NVM_HOMEBREW NVM_LAZY nvm_completion
56+unset NVM_HOMEBREW NVM_LAZY NVM_AUTOLOAD nvm_completion