Last active
February 20, 2026 18:18
-
-
Save fadookie/e0d38b9a5b71965788d1e59779c73448 to your computer and use it in GitHub Desktop.
NVM automatic use for zsh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # NVM automatic use - based on https://stackoverflow.com/a/39519460 | |
| # but modified to only log that a missing version should be installed instead of trying to auto-install it. | |
| # Copy / source this into your ~/.zshrc or similar profile. | |
| # Source - https://stackoverflow.com/a/39519460 | |
| # Posted by Rotareti, modified by community. See post 'Timeline' for change history | |
| # Retrieved 2026-02-20, License - CC BY-SA 4.0 | |
| 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 | |
| local requested_version=$(cat "$nvmrc_path") | |
| echo "Please install Node $requested_version" | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment