Developer Pro Tips
1 min read
Node
1. Use nvm
to manage Node versions
If you work on multiple projects with different Node versions, nvm
is a must-have. It helps manage the Node versions
on the machine. This is especially useful for teams, working on projects using different Node versions.
Auto-switching Node versions
If you’re using zsh
, you can add the following to your .zshrc
to automatically switch Node versions when you cd
into a directory with a .nvmrc
file.
# Automatic `nvm install / use` when .nvmrc detected in cwd
# https://stackoverflow.com/questions/23556330/run-nvm-use-automatically-every-time-theres-a-nvmrc-file-on-the-directory
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
nvm install
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
2. Use corepack
Corepack is an experimental tool to help with managing versions of your package managers. It exposes binary proxies for each supported package manager that, when called, will identify whatever package manager is configured for the current project, download it if needed, and finally run it.