The Node Version Manager (NVM) is an indispensible tool for JavaScript Developers. Here’s why it’s so useful, and how to use it in Linux.
What is Node.js
Node.js is a JavaScript runtime which allows you to build and run JavaScript apps outside of the web browser. It’s commonly used to build APIs, webapps and even desktop applications.
New versions of Node.js are released periodically, with major version releases often breaking compatibility with code written for earlier versions.
Managing Code Dependencies Sucks
A code dependency is any software or library which your project depends on – if you’ve built an app on Node.js, then the specific version of Node.js you used while developing your project is a dependency. Other dependencies will include the specific versions of any third party libraries you have used, and may also include tools like compilers or other applications.
Managing code dependencies is never fun. Some of your dependencies will have their own dependencies, so once you’ve got your project up and running and working, you want to make sure things remain consistent so that no changes break your code.
For example: You’ve built a cool app in Node.js – and deployed it to production and moved on to another project. In the mean time, a new version of Node.js has been released and you’ve updated to that and are using it in your new projects.
But – something breaks in your old app. You go to run it and it fails – it depended on the old version of Node.js. Rolling back to the old version would be a big hassle to fix a minor bug. Frustration ensues.
NVM To The Rescue
NVM is the Node Version Manager – it does just what the name says – it manages the versions of Node.js on your system.
With a few terminal commands you can switch the version of Node.js you system is using, including the version of any installed modules. Now all you need to do is make a note of which version of Node.js you used for a particular project, and switch to that version using NVM the next time you want to fire it up and fix something. Neat!
Installing Node Version Manager on Linux
NVM can be quickly installed in Linux. Rather than provide installation instructions here, I’ll link to them, just in case they change with a future update.
Click here for NVM installation instructions for Linux.
If you have an existing Node.js environment you may want to migrate your existing global Node.js packages after installing.
Using NVM on Linux
Here’s how to use NVM on Linux.
Installing a Specific Version of Node.js
The below command will install a specific version of Node.js:
nvm install 14.7.0
Switching Node.js Versions
You can switch the active version using the use command:
nvm use 14.7.0
Setting the Default Version
By default, the first version of Node.js which is installed will become the default – this can be changed by changing the default alias:
nvm alias default 14.7.0