Sailing with Node

Managing Node.js Versions - A Step-by-Step Guide to Switching Between Projects with NVM

May 16, 2021 Dykraf

Manage multiple projects with different versions of NodeJS on your local machine with NVM (Node Version Manager).

Web Story VersionWeb Story

Node v14.0.0 is out and this time I want to show I managed to change Node to different versions. Changing the Node.js version between projects using NVM really help me to handle and switch between multiple project versions. It really increases the power of the Node.js ecosystem. This is essential for me because some of my projects run in different versions. I am going to show you How to Change Node.js Version Between Projects Using NVM (Node Version Manager).

Many of my projects have been based on the Node.js ecosystem — Gulp.js, Express.js, and other libraries like React.js developed in the Next.js framework. For quite some time they updated and released new versions and several of them required upgrading the Node.js version. So, this article is about how to update the Node versions between projects.

Unless you already use Docker container projects, you’ll be fine with your Node version. But that’s another different topic from this one that probably I’ll probably share it with you too in the future. This is my story about how I am using NVM on top of all my Node.js projects in my macOS machine.

Note: If you already have a previous Node.js in your machine, that version will still available for you to use as the default version on your machine, in case you decide to use and install NVM.

Default Node.js version in my MacOS

Installing NVM

  1. Get NVM/Node Version Manager using brew: brew install nvm in your MacOS terminal console.
  2. Get NVM / Node Version Manager using curl:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

Or wget:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

Set Up NVM

  • Close and reopen terminal and type source ~/.bash_profile or source ~/.profile to make sure NVM included in your profile.
  • Then type export NVM_DIR=~/.nvm and source $(brew --prefix nvm)/nvm.sh for setting up NVM directory and shell script.
  • Here’s the .bash_profile that’s already set up for installing NVM in the system:

NVM in .bash_profile

  • Restart terminal and verify that NVM was properly installed by typing nvm --version in the terminal:

NVM Version

  • Install multiple Node version from 8 or 12 using the command nvm install v8.11.0 or nvm install v12.6.0 in the terminal:

NVM install v12.16.0

  • List installed versions using the command nvm list or nvm ls, the arrow indicates the current Node.js version:

NVM list all Node.js versions installed

  • Using or switching node versions using NVM nvm use v8.11.0 or nvm use v12.16.0 and verifying the current node node -v is done the same way whatever your choice is:

NVM use v8.11.0 and check Node version NVM use v12.6.0 and check Node version

Note: nvm use command is only available on each terminal when you open up in your project. If you close all terminals and open a terminal again, the default version will always take charge. To overcome this, but without losing the older version, you could just set up an .nvmrc file, or you could set up a node default alias to change the current default version available to any shell terminal you open.

Project Set Up for NVM in the .nvmrc File

NVM provides a simple shortcut helper that enables each of your projects to recognise which version of your Node.js is in usg, using a file name .nvmrc to put it in your project root directory.

  • Open up your editor in project directory, create and save a file named .nvmrc in the root directory. Type your default or desired Node.js version, such as v10.13.0 or v8.0.6, to continue.

nvmrc file containing the desired Node version

  • Now, that you already have a .nvmrc in your root directory that defines the default Node.js version for your project, you could just type nvm use to tell nvm to switch the version to use before you start developing.

NVM use command for switching Node version

  • You could add a .nvmrc in all of your Node.js root directory and write down the default version used in that project. Switch the Node.js version by typing nvm use in the terminal.

nvmrc in the project root directory

Set Node.js Default Aliases in NVM to available in Any Open Shell

After you install the necessary Node.js versions using NVM, you can now create a default alias for the version installed. The NVM alias will work only after you installed Node.js with NVM.

Let’s say you want to use Node.js v10.16.3 as your default version. Just type nvm alias default v10.16.3 in your terminal and then type nvm use default. This command will make v10.16.3 available in any shell you open —- you just have to restart your terminal to make sure it works. You can switch back to any other default version using that command again and it will immediately available in any open shell.

Already created aliases will be available in the terminal using the command nvm use default. Switching other aliases available in examples; just type nvm use stable or nvm use node or nvm use system for selecting the system’s default version that NVM already installed.

$ nvm ls — List all the available Node Version.

$ nvm alias — List all available alias

$ nvm alias default v10.16.3 — Set Node default version to v10.16.3

Restart all Terminal and check the Node version default change to v10.16.3 version

$ nvm alias default — Changing between Node version using aliases in NVM and set back to default version

Install Node v14.0.0 using NVM

Type nvm install node in the terminal, this will install the latest version of Node. Alternatively, just type the version nvm install v14.4.0 in the terminal.

$ nvm install node — Will install the latest Node version v14.4.0

$ nvm install v14.4.0 — Will install the latest Node version v14.4.0

Note: If you install v14.0.0 or another version and quit all terminals, that version will not be available as the default of your node. Remember, we already set a global node version in v10.16.3. You need to set v14.0.0 as the default Node version to your machine and execute nvm use default.

Set Node v14.0.0 as the Default Version

Quit all terminals, list all versions, and check the default version. The default Node version was still in v10.16.3 (*Notice the arrow indicates the Node current default) version that we previously set. Also, notice that v14.0.0 was already installed and listed in the version list. To change the default version to v14.0.0, type nvm use default v14.0.0 and nvm use default then restart all terminal instances. Finally, check the version node -v that was already set up to v14.0.0 for use at any time.

nvm ls — Check all available versions with the new v14.0.0 version nvm use default v14.4.0 — Set up v14.0.0 as the Node default version Close all terminal and check your fresh default v14.0.0 Node version

Closing

Thanks for reading. I hope this can help you to switch between Node.js versions for your web development projects. NVM has really helped me through this kind of situation. Now that I’ve already moved on to Docker containers, NVM will always be the tool that I can count on.

Have a good day!

References: https://github.com/nvm-sh/nvm

Originally posted in: How to Change Node.js Version Between Projects Using NVM

Topics

Recent Blog List Content:

Archive