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
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)
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
We can change the default Node version for every new terminal session by running:
$ nvm alias default 8.0.0
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)
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)
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!
Top comments (12)
Welcome to Dev.to. Wonderfully written and very useful. I use nvm all the time. Especially when testing my fdir library that supports multiple versions and it needs to be tested on all of them.
Hello Abdullah!
Thanks for your warm welcome to this community. I can see that you also recently joined, so I wish you same back :)
Link to your library doesn't work, but I managed to find it anyway.
I'm glad that you found NVM useful, because it means that this post's goal to spread the word about it isn't wrong.
Thanks for your comment!
Thanks a lot, Maroje! Yes, I mistyped the URL, haha. It's fixed now. Hope you liked the idea.
Indeed. There are many people out there who still do not know anything about version managers. This post is a good introduction. Keep it up!
: )
What about using asdf-vm (asdf-vm.com/#/)? I hate to have multiple version managers to each tool. This handles all together.
Hi Fran!
I haven't heard about that tool before, it seems really useful if you're working with multiple languages. If you used it, do you think it's not an overkill even if you're working with only 1 or 2 programming languages in total?
Thanks for your comment :)
Hi,
I think that using something like asdf-vm and learn only one tool makes more sense than learning multiple tools that "do the same" and learn each particularity of each tool.
If you do a lot of switching (ie. using nvm use on every shell creation) i highly recommend fnm, which is A LOT faster.
Link: github.com/Schniz/fnm/
Hi Pawel!
Thanks for your tool recommendation.
One question, what actullay makes fnm faster in comparison to nvm?
I see many similarities in installation process & available commands, so switching from nvm to fnm should also be easy, if someone decides to do so.
Im not really sure why its faster. I assume its just doing much less things underneath.
I found it when i added "nvm use" to my zsh_profile and every terminal in VSCode was running it... and it took over 1 second to open a terminal. I dont remember the exact numbers, but it was over 10x faster when doing that one thing. :)
Of course, keep in mind that i measured on my old computer, maybe on yours its not a factor at all :)
I use it too for the same reasons! I highly recommend it.
I'ver just created a much better version managers for multiple SDKs.
gvcgo.github.io/vmrdocs/
What makes it better in comparison to NVM?