Managing my node versions

Search for a command to run...

Have you ever wondered how to set a hotkey to focus an input field in your React application? Follow along to learn how. We'll even handle showing different hotkeys based on whether the user is on a Mac or Windows system. Implementation const SearchI...

As a software engineer, choosing and understanding your text editor is important part of your work, as it impacts your productivity and workflow efficiency. It's like choosing the perfect tool for any trade - you need to know what tool to use and how...

Design systems are essential tools for software engineers as they provide a structured framework for maintaining consistency and coherence in product design. By offering clear guidelines and standards, design systems streamline the development proces...

Let's Build our own real-time page view tracker using Next.js as the frontend framework and a Postgres database hosted by Supabase. One of the best ways to understand how your blog posts are performing is by tracking page views. You can begin to unde...

One of the best ways to drive traffic to your website is to have strong Search Engine Optimization (SEO). You can provide search engines with all the URLs for your website using a Sitemap. This allows for easier indexing and more efficient crawling b...

Working on multiple projects at a time, I typically have to switch what version of node I am running. For work, the front end code is using NodeJS v10 and our API micro-services are using NodeJS v8. My personal projects are using NodeJS v12, and sometimes I like to play around with bleeding edge features on the newest and latest version.
Since I am constantly requiring different node versions, I need a tool to make this process easy.
I currently develop my personal projects using Ubuntu. I could easily install node using apt.
sudo apt install nodejs
This won't work for me. How would I switch between all the different node versions? You can install specific versions using apt but you cannot switch easily. It would also take more work to lock your apt version down so whenever you perform an update across your system, it doesn't update node without you realizing it.
I also do not think we should be installing NodeJS with sudo permissions. You are now giving NodeJS full control over your computer. NPM installs third-party modules and it can contain any script the provider wants. How can you trust a third-party script with full control? Unless you read every module and submodule's code before you install it, there might be something malicious. I would rather not take the risk and stay away from sudo.
You could download the binary directly from the NodeJS website and place it in your ~/.local/bin folder and make sure that folder is in your $PATH but you still cannot manage different versions easily.
I have been using NVM to managing my node version for the past 4 years and it's one of the first things I install on a new computer.
Installation is fairly simple. They provide a one line copy and paste to install and set up your environment.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
# Version might be difference since article was published
When the above script runs, it installs nvm in ~/.nvm and adds the initializer for your terminal (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
# Initializer
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
After the script finishes, all you need to do is restart your terminal and it should work. You can verify by typing nvm --version or command -v nvm.
If it does not work, nvm provides troubleshooting steps for Linux and macOS
Once you have nvm installed and working, it's easy to manage node.
You can easily install whatever version you want using nvm install
# For most recent version of nodejs v12
nvm install 12
# You can be more specific
nvm install 12.18.3
To switch node version you can call nvm use
# To enable node 12
nvm use 12
# to enable node 8
nvm use 8
If your folder contains a .nvmrc file you can just run nvm use and it will pull the version from .nvmrc.
To trigger nvm use automatically, you can use another package called AVN or a lightweight script offered by nvm.
Every time you start a new terminal, NVM automatically sources your default node version. When this process runs, it calls npm config get prefix. This command has some performance issues and there has been a ticket create explaining the reason.
There are two solutions that help with terminal performance. You can remove the default node version using nvm unalias default or when you source nvm you add --no-use.
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh --no-use"
NVM will no longer source node at startup and you will have to manually run nvm use before running anything with node.
I have been using NVM for the past 4 years. It has given me all the tools I need to update and manage my node versions safely. The installation is simple and straightforward. I have never had any problems with it and use it almost daily.