For developers, Node.js is an essential runtime environment. However, working on multiple projects simultaneously often leads to a common headache. Project A might require Node.js version 18, while Project B demands the latest version 22. Constantly uninstalling and reinstalling Node.js in such scenarios is the epitome of inefficiency.

The elegant solution to this version conflict problem is NVM (Node Version Manager). NVM allows you to install multiple, independent versions of Node.js on a single system and switch between them freely with simple commands. Today, we'll explore how to install and use NVM to maximize your development productivity.

1. Installing NVM: The First Step to Flexibility

NVM is installed by downloading and executing an installation script directly from GitHub using the curl command.

First, visit the NVM project's GitHub page to find the latest installation command. It's a crucial security practice to audit scripts before running them. You can do this by running the command without the final | bash pipe to review the script's contents.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh

Once you've reviewed the script and are comfortable with it, run the command again with | bash appended. This command will install NVM to your user account.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

After the installation is complete, you need to reload your .bashrc file so that your terminal session recognizes the nvm command.

source ~/.bashrc

2. Installing and Managing Node.js Versions

Now you're all set to use NVM. Let's see how easily you can manage your Node.js versions.

Listing Available Versions

First, let's check which versions of Node.js are available for installation.

nvm list-remote

You'll see a long list, from stable LTS (Long Term Support) versions to the latest releases.

Installing a Specific Version

You can pick and install any version from this list. For example, let's install the LTS version v22.16.0.

nvm install v22.16.0

Listing and Switching Installed Versions

To see the versions installed on your machine, use the following command.

nvm list

The -> arrow points to the currently active version. If you want to use a different version, you can easily switch with the use command.

# Install the lts/fermium (v14.21.3) version
nvm install lts/fermium

# Switch to version v22.16.0
nvm use v22.16.0

After switching, you can verify the current version with node -v and see the change reflected immediately.


3. Maximizing Efficiency with a Default Version

If you want to use a specific version every time you open a new terminal, you can set a default version.

nvm alias default v22.16.0

Now, whenever you start a new terminal session, Node.js version v22.16.0 will greet you by default.

Conclusion

NVM is more than just a tool for installing multiple Node.js versions; it's an essential development utility that liberates us from version compatibility issues and helps build optimal development environments for each project. By using NVM, you can stop worrying about version problems and enjoy a smarter development experience focused purely on your code.

Share this post

Written by

Alex
"Technology doesn't have to be complicated. The best tech is the kind you forget is even there."

Comments