Switching between multiple Node versions with Node Version Manager

Maroje Macola - Mar 16 '20 - - Dev Community

Hello dev.to readers!

Intro

This is my very first post here and hopefully many more will be written. Even though I've worked only on PHP projects, it turned out that my first post here is about switching between Node versions by using the handy tool called Node Version Manager.

Setup

Package usage is intended for MacOS or Linux, but there are some alternatives for Windows, like nvm-windows and nodist. Those alternatives are created by other developers, so there may be some similarities with features, but carry on with reading at your own risk 😁

To keep this post shorter and focused more on NVM's features, I will skip steps for installation. Nevertheless, here is a link for setup, if you're using Homebrew (it helped me!).

Tool usage

After installation, you can check which Node versions you have installed by running:

$ nvm ls
Enter fullscreen mode Exit fullscreen mode

There should be none, at first. So, let's install newest Node version:

$ nvm install node
...
Now using node v13.10.0 (npm v6.13.7)
Creating default alias: default -> node (-> v13.10.0)
Enter fullscreen mode Exit fullscreen mode

As we can see, when installing first Node version, it also automatically creates an alias for default version.
To install specific Node version, run:

$ nvm install 8.0.0
Enter fullscreen mode Exit fullscreen mode

We can change the default Node version for every new terminal session by running:

$ nvm alias default 8.0.0
Enter fullscreen mode Exit fullscreen mode

Now that we have multiple Node versions installed, we can start switching between them by simply running:

$ nvm use node
Now using node v13.10.1 (npm v6.13.7)
$ nvm use 8.0.0
Now using node v8.0.0 (npm v5.0.0)
Enter fullscreen mode Exit fullscreen mode

Simple, right? Do you see some potential benefits from this? Like running different Node versions in multiple terminals at the same time? It's really awesome.

Tips

For those reading all the way down here, here is a tip regarding aliases: how about naming them after your project’s name? So you don’t need to think of, or search for project’s specific version. For example, here is an example of my alias:

$ nvm alias eotg 8.0.0
eotg -> 8.0.0 (-> v8.0.0)
$ nvm use eotg
Now using node v8.0.0 (npm v5.0.0)
Enter fullscreen mode Exit fullscreen mode

Another extra tip/warning if you're using PHPStorm (or probably any other IDE):
If you need to run npm install while running specific Node version, it's not enough to just set Node version with nvm use 10.1.2. It's also necessary to set the correct Node version in your PHPStorm's preferences, for its Node interpreter. This bugged me longer than expected, so I'm writing it in hope that it might save someone the trouble 😌

Conclusion

Node Version Manager is a simple, yet powerful tool which can save us a lot of time & trouble. Here are only a number of commands to get you started and make you see the value behind it.

If you have some other tips, suggestions and/or experiences to share about Node Version Manager, let everyone else know in the comments below!

Cover image source - https://miro.medium.com/max/1050/0*csTuUtvi1VdLS4le.jpg
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player