Let ZSH automatically update your node version

because typing `nvm use 19.3.0` is so 2011

I switch between projects all day.
The projects I am switching between use different node versions.
Here is an example of what it looks like when I switch projects.

cjb/really_good_code/older_project
  $ cd ../shiny_new_project
cjb/really_good_code/shiny_new_project
  $ bin/dev
[ERROR] [plugin copy-for-admin] inputPath.replaceAll is not a function
  53 β”‚ ... dehash = inputPath => inputPath.replaceAll(replaceHashRegex, '');
πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯ EVERYTHINGS BREAKING πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯
This error came from the "onEnd"  This error came from the "onEnd" callback registered here:
  error Command failed with exit code 1.
...
πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯ DEPRECIATED / NEVER WORKED  πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯
Exception raised: RuntimeError
Command failed with status (1): [yarn run esbuild-dev...]
πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯ ITS ALL WRONG ?? p > np  πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯
...
cjb/really_good_code/shiny_new_project
  $ nvm use '19.3.0'
cjb/really_good_code/shiny_new_project
  $ bin/dev
[Server] * Puma version: 5.6.5 (ruby 3.0.2-p107) ("Birdie's Version")
[Server] * PID: 18036
[Server] * Listening on http://0.0.0.0:4000
[Server] * 😎 You are awesome. This project works great.
[Server] Use Ctrl-C to stop

Additionally I LOVE to forget CLI commands, and version numbers.
No one forgets a CLI command, quite as quickly as I do.
(Weird flex, but ok?)

SO I let my zshell manage my node version manager. Simply plop this little bash script at the bottom of your ~/.zshrc

# .zshrc
# Change node version based on .nvmrc
function change_node_version {
	nvmrc="./.nvmrc"
	if [ -f "$nvmrc" ]; then
		version="$(cat "$nvmrc")"
		nvm use $version
	fi
}
add-zsh-hook chpwd change_node_version
change_node_version

Now simply ensure there is an .nvmrc file at the root of all of your projects. It should look something like this.

19.3.0

And there ya go. Switching projects looks more like this

cjb/really_good_code/older_project
  $ cd ../shiny_new_project
Now using node v19.3.0 (npm v9.2.0)
cjb/really_good_code/shiny_new_project
  $ bin/dev
[Server] * Puma version: 5.6.5 (ruby 3.0.2-p107) ("Birdie's Version")
[Server] * PID: 18036
[Server] * Listening on http://0.0.0.0:4000
[Server] * 😎 You are awesome. This project works great.
[Server] Use Ctrl-C to stop

Now I spend way more time doing what I do best.
$ heroku run --? --verbose --help

If you’re looking for a team to help you discover the right thing to build and help you build it, get in touch.

Published on November 30, 2023