While installing nvm via Homebrew, I noticed a warning about setting $NVM_DIR. By default, Homebrew installs nvm to a versioned path like:
/opt/homebrew/Cellar/nvm/0.40.3
If you leave $NVM_DIR pointing there, any Node.js versions or global npm packages you install with nvm will live in that same directory. The catch: when you upgrade or reinstall nvm, Homebrew may replace or remove this directory, and you’ll lose everything stored there.
To avoid this, make sure $NVM_DIR points somewhere stable, like $HOME/.nvm. Homebrew’s nvm installation already includes the correct snippet in .zshrc:
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"
That keeps your Node versions and global packages safe during upgrades. If you’ve already installed Node before making the change, you’ll need to reinstall them under the new $NVM_DIR.
References: