DEV Community

Cover image for Automate Your Node.js Version Switching
Krisztián Maurer
Krisztián Maurer

Posted on

Automate Your Node.js Version Switching

Imagine you're working on a project using Node.js. But suddenly, your project doesn't work. After some time spent figuring out the problem, you discover you were using the wrong version of Node.js for your project. You switch to the correct version, and everything starts working again.

Now, think about someone who works on many different projects, each needing a different version of Node.js. Every time they move to a different project, they have to remember to switch to the right version. This can be really annoying and slow them down.

Image description

This article is like a magic trick for fixing that problem. It shows how to set up your terminal to automatically change to the right version of Node.js for whatever project you're working on. If you use Bash or Zsh, you can make it so that every time you open a project, your terminal checks to see which version of Node.js that project needs. If you don't have it yet, your computer will even download it for you. This means you can jump from one project to another without having to stop and think about which version of Node.js you need. It's all taken care of for you, making everything much smoother and letting you focus on the fun part: creating stuff.

If the node version you need isn't found and has to be installed, here's what it will look like:

Image description

To get started, the first step is installing nvm. It's a simple command:

npm i -g nvm
Enter fullscreen mode Exit fullscreen mode

Image description

With nvm installed, I will guides you through the process of adding specific scripts to your .bashrc or .zshrc file. These scripts enable your shell to automatically find the .nvmrc file in your project directory and switch to or install the Node.js version it specifies. This automation simplifies the workflow for developers, making it easier to manage multiple projects with different Node.js version requirements.

Note: This trick works on my Ubuntu setup. Not sure how to make it happen on Windows, though!

BASH

Pop open your .bashrc file with vi ~/.bashrc, and then add this script to the end of your file... wait, how do you escape this text-editing black hole? Ah, just type :wq to wave goodbye. But hey, if you're not into memorizing escape spells, maybe give nano a try for editing files sudo apt-get install nano then nano ~/.bashrc. It's like the friendly neighbor of text editors —always there to lend a hand without the cryptic commands.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion


nvm_auto_use() {
  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
}
export -f nvm_auto_use
PROMPT_COMMAND="nvm_auto_use; $PROMPT_COMMAND"
Enter fullscreen mode Exit fullscreen mode

Then run source ~/.bashrc. Done.

ZSH

For those rocking the Zsh terminal, setting things up is a similar, just swapping out for another script. And you probably don't need the whole "how to escape from vim" saga. To get started, just add this to the end of your ~/.zshrc file:

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

Enter fullscreen mode Exit fullscreen mode

Then run source ~/.zshrc. Done.

Thanks for sticking around till the end! I hope you found this guide useful.

Top comments (0)