In the early 2000s, Javascript was used only as a sprinkler to put some client-side magic, such as Form Validation, etc., in web applications thus, to implement server-side functionality, such as interacting with databases, etc., a different programming language had to be used.
This often led to the need for separate development teams for the backend and frontend, resulting in increased development time and costs.
Also, many web developers who got accustomed to writing Javascript code started to ask the community, “how can Javascript code be executed on my PC”? See this StackOverflow thread from 2010 below:-
https://stackoverflow.com/questions/2941411/executing-javascript-without-a-browser
On May 27, 2009, a Ruby developer, Ryan Dahl, released the initial version of a Javascript runtime or interpreter NodeJS, based on Chrome’s Javascript V8 Browser Engine, which allowed developers to execute Javascript code without a browser.
Thus, NodeJS became the solution to the above problems, and software development companies started using NodeJS for web development.
In this article, I will guide you on how to install NodeJS on your PC and also how to change NodeJS versions for developing web applications for different NodeJS versions.
How to Install NodeJS?
NodeJS can be installed in different ways on all major operating systems Linux, Mac, and Windows. The table below guides you to pick the best way for you:-
Platform | Installation Methods | Popularity | Benefits | Features |
Linux | 1. Package Manager (e.g. apt-get, yum, pacman, etc)
2. Binary archives (.tar.gz) 3. nvm |
High | Easy to install, updates available through package manager | Multiple versions can be installed and managed through nvm |
Mac | 1. Homebrew
2. Mac installer (.pkg) 3. nvm |
High | Easy to install and use | Multiple versions can be installed and managed through nvm |
Windows | 1. Windows installer (.msi)
2. Chocolatey 3. nvm-windows |
Low | Easy to install with Windows installer, package manager available through Chocolatey | Multiple versions can be installed and managed through nvm-windows |
You can further go to NodeJS’ official download page to download a compatible installer for your operating system.
Mac
If you are on Mac then you can download the .pkg file and double-click it to run and install NodeJS on your MacBook.
Windows
If you are on Windows then you can download the .msi file and double-click it to run and install NodeJS on your Windows PC.
Linux
If you are a Linux person and use Ubuntu then you can run the following command to install NodeJS on your Ubuntu system.
curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\ sudo apt-get install -y nodejs
You can follow the instructions here to install NodeJS if you use Debian or if you use any other flavors of Linux.
An alternate method to install NodeJS – NVM
However, I prefer using an alternate method for installing NodeJS than using my operating system’s package manager. As you can see in the table above, an installation method called ‘nvm’ is also mentioned to install NodeJS.
‘nvm’ is called Node Version Manager and is used primarily to install NodeJS as it provides extra features such as changing node version which the default installation methods do not provide.
NVM is a savior when I need to change my NodeJS version to test my React web application for different NodeJS versions.
What is NVM – Node Version Manager?
As a Javascript developer myself, managing multiple projects often requires me to install and manage specific NodeJS versions.
Also, sometimes I need to test my NodeJS web application to see if it is compatible with any previous versions (or latest development releases) of NodeJS.
To manage and install specific Node.js versions, I turn to Node Version Manager (NVM).
NVM is a shell script, or a popular environment manager for NodeJS, that automates the installation and management of multiple Node.js versions on a single machine, making it a convenient tool for my development projects.
I find NVM’s command-line interface (CLI) easy to use for installing, switching between, and removing different NodeJS versions.
Can different versions of NodeJS be installed and managed without NVM?
Well, NVM is the preferred method to install and manage different versions of NodeJS for many, however, a Javascript developer, Daniel Young, created his own shell script to install and switch node versions without NVM.
The above shell script, however, lacks the comprehensive features of NVM such as bash completion, version lookup, etc.
Is NVM similar to NPM, Yarn, or PNPM?
No, NVM allows you to install and manage different NodeJS versions on your system whereas NPM (Node Package Manager), Yarn, and PNPM (Performant NPM) allow you to install and manage different Node modules and 3rd party packages.
You can install and manage different 3rd party Javascript programs such as middlewares, ORMs, Backend Queue Managers, etc using NPM, Yarn, and PNPM. I personally use both NPM and Yarn.
A few techie friends of mine prefer using PNPM over NPM due to its speed and efficiency.
You can compare NPM, Yarn, and PNPM by going to this feature comparison on PNPM’s website.
How to change the NodeJS version by using NVM?
Here’s a step-by-step guide on how to change your NodeJS versions using NVM:-
Install Node Version Manager (NVM)
To install the latest version 0.39.3 of NVM as of 13/03/2023, open your terminal and run the following command:-
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
The above command will download and run the NVM installation script installing the latest version of Node Version Manager (NVM) on your system to help you with changing of NodeJS versions in your workflow during NodeJS development.
Once the installation is complete you can check if NVM is installed on your system by running the following command in your terminal:-
nvm -version
List all NodeJS versions available to install by using NVM
You can list all the NodeJS versions available to install on your system. I prefer using ‘LTS’ Long Term Support versions as they tend to be more stable and are supported for longer periods of time helping you to maintain your NodeJS app more efficiently.
Run the following command to list all NodeJS versions:-
nvm ls-remote
Run the following to list only LTS NodeJS versions:-
nvm ls-remote --lts
Install the latest NodeJS version by using NVM
Run the following command:-
nvm install 19.7.0
Install the latest LTS NodeJS version by using NVM
Run the following command:-
nvm install --lts
List all the NodeJS versions installed by using NVM on your system
Run the following command to list all installed NodeJS versions on your system:-
nvm list
As you can see in the above image we have 3 different versions of NodeJS installed on the system. NVM installs a compatible version of NPM too during the installation of a NodeJS version. Let us try changing the NodeJS versions using NVM next.
Which current NodeJS version are you using right now?
Let us see the current NodeJS and NPM versions in use on your system:-
node --version
Let us change the NodeJS version to the latest by using NVM:-
nvm use 19.7.0
As you can see above we can easily change the NodeJS versions on our systems.
Remember this change is NOT persistent for the user you are logged in as on your system i.e. the moment you close this terminal window the NodeJS version falls back to default unless you change it using NVM again inside the new terminal window.
How to set a default NodeJS version for your system?
Let us assume we want to use the latest LTS NodeJS version as default. And, to do so run:-
nvm alias default 18.15.0
Now, to see the change close your current terminal window and open a new window, and then run:-
nvm list
Now, whenever you will open a new terminal window or a tab then your NodeJS version will be the default one you set up above. To change the default NodeJS version you can run the nvm alias default NODEJS_VERSION command again inside your terminal.
How to set up a default NodeJS version project-wise?
Many times you will need to use different NodeJS versions for your different projects but with the current setup, we will have to manually change the NodeJS version by using NVM for each project.
To solve this, NVM checks, the moment you cd PROJECT_NAME inside the terminal, if a NodeJS project has a .nvmrc file in its root folder to see the project’s default NodeJS version, and then automatically uses (and installs too if not already installed) the specified NodeJS version.
my-project/ ├── .nvmrc ├── node_modules/ ├── package.json ├── src/ │ ├── index.js │ └── components/ │ ├── App.js │ └── Header.js └── yarn.lock
Conclusion
In this article, I have explained how to install NodeJS using the official method, and also introduced an alternative method to manage different versions of NodeJS using NVM (Node Version Manager).
With NVM, you can effortlessly switch between different versions of NodeJS and install multiple versions simultaneously without any conflicts. This flexibility is crucial, especially when adhering to a NodeJS job description that may require compatibility with various NodeJS versions.
It’s important to note that NVM is distinct from package managers such as NPM, Yarn, or PNPM, which are designed to manage packages and dependencies within a specific version of NodeJS.
FAQ
Was NodeJS the first to make Javascript execute outside the browser?
No, there were many other alternatives to NodeJS or other ways to run Javascript on the server side such as ASP, JSP, Spidermonkey, and Rhino Javascript Engine, however, either they became obsolete or couldn’t solve the developer’s problems.
Are there any alternatives available for NVM?
Yes, there are 4 best alternatives to NVM, which are:-
NVS: Offers efficient and automatic version switching, with a consistent and easy-to-use command-line interface
Nodist: A good choice for Windows users
N: A lightweight and easy-to-use option
Volta: Provides a more holistic development environment
However, NVM is the most popular and widely used option for installing, managing, and changing NodeJS versions on a single system.
How do I set up NVM on Windows?
NVM is not available for Windows systems, however, there is a program called nvm-windows which you can download from https://github.com/coreybutler/nvm-windows and go through its README to start changing NodeJS versions on your Windows PC.
If you enjoyed this article, check out our other guides below: