Alterar versão do nó: um guia passo a passo |  Todos os ambientes

Change Node Version: A Step-by-Step Guide | All environments

Learn how to change Node version like a pro across all versions and environments covered. Take your skills to the next level and stay ahead of the game.

Imagem em destaque

In the early 2000s, Javascript was just used as a sprinkler to put some client-side magic like form validation, etc., into web applications, so to implement server-side functionality like interacting with databases. data, etc., a different programming language had to be used.

This often led to the need for separate development teams for backend and frontend, resulting in increased development time and costs.

Furthermore, many web developers who got used to writing Javascript code started asking the community: “how can Javascript code run on my PC”? See this StackOverflow thread from 2010 below: –

On May 27, 2009, Ruby developer Ryan Dahl released the initial version of a NodeJS Javascript runtime or interpreter based on Chrome's V8 Javascript browser engine, which allowed developers to run 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 to develop web applications for different NodeJS versions.

How to install NodeJS?

NodeJS can be installed in different ways on all major Linux, Mac and Windows operating systems. The table below guides you to choose the best way for you: –

Platform Installation methods Popularity Benefits Characteristics
Linux 1. Package manager (e.g. apt-get, yum, pacman, etc)

2. Binary files (.tar.gz)

3. nvm

High Easy to install, updates available through the package manager Multiple versions can be installed and managed through nvm
Mac 1. Homemade beer

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. Chocolate

3. Windows NVM

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 Official NodeJS Download Page to download a compatible installer for your operating system.

Mac

If you are on Mac, you can download the .pkg file and double-click it to run and install NodeJS on your MacBook.

windows

If you are on Windows, 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 user and use Ubuntu, you can run the following command to install NodeJS on your Ubuntu system.

 curl -fsSL 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 some other flavors of Linux .

An alternative method to install NodeJS – NVM

However, I prefer to use an alternative method to install 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 mainly used to install NodeJS as it provides extra features like changing node version which standard 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, managing multiple projects often requires me to install and manage specific versions of NodeJS.

Also, sometimes I need to test my NodeJS web application to see if it is compatible with any previous versions (or newer development releases) of NodeJS.

To manage and install specific versions of Node.js, I turn to Node Version Manager (NVM).

NVM is a shell script, or popular environment manager for NodeJS, that automates the installation and management of multiple versions of Node.js 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, and removing different versions of NodeJS.

Can different versions of NodeJS be installed and managed without NVM?

Well, NVM is the preferred method for installing and managing different NodeJS versions for many, however, a Javascript developer, Daniel Young, created his own shell script for installing and switching node versions without NVM .

The above shell script, however, lacks NVM's comprehensive features like bash completion, version lookup, etc.

Is NVM similar to NPM, Yarn or PNPM?

No, NVM allows you to install and manage different versions of NodeJS on your system, while NPM (Node Package Manager), Yarn and PNPM (Performant NPM) allow you to install and manage different Node modules and third-party packages.

You can install and manage different third-party Javascript programs like middlewares, ORMs, backend queue managers, etc. using NPM, Yarn and PNPM. I personally use NPM and Yarn.

Some of my friends prefer to use PNPM instead of NPM because of its speed and efficiency.

You can compare NPM, Yarn, and PNPM by accessing this resource comparison on the PNPM website.

How to change NodeJS version using NVM?

Here is 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- 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 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 for installation using NVM

You can list all NodeJS versions available to install on your system. I prefer to use 'LTS' long-term support releases as they tend to be more stable and supported for longer periods of time, helping you maintain your NodeJS application 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 version of NodeJS using NVM

Run the following command: –

 nvm install 19.7.0 

Install the latest LTS NodeJS version using NVM

Run the following command: –

 nvm install --lts 

List all NodeJS versions installed using NVM on your system

Run the following command to list all NodeJS versions installed on your system: –

 nvm list 

As you can see in the image above we have 3 different versions of NodeJS installed on the system. NVM also installs an NPM-compatible version when installing a NodeJS version. Next, let's try changing NodeJS versions using NVM.

What current version of NodeJS are you using now?

Let us see the current versions of NodeJS and NPM in use on your system:-

 node --version 

Let's change the NodeJS version to the latest using NVM:-

 nvm use 19.7.0 

As you can see above, we can easily change NodeJS versions on our systems.

Remember that this change is NOT persistent for the user you are logged into your system as, i.e. the moment you close this terminal window, the NodeJS version goes back to the default unless you change it using NVM again within the new terminal window.

How to set a default NodeJS version for your system?

Let's assume we want to use the latest LTS NodeJS version as the default. And to do this, run: –

 nvm alias default 18.15.0 

Now, to see the change, close the current terminal window, open a new window and run: –

 nvm list 

Now, whenever you open a new terminal window or tab, your NodeJS version will be the default one you configured above. To change the default NodeJS version, you can run the nvm alias default NODEJS_VERSION command again inside your terminal.

How to configure a default version of NodeJS in terms of project?

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 using NVM for each project.

To resolve this, NVM checks, the moment you cd PROJECT_NAME inside the terminal, whether 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) also if not already installed) the specified version of NodeJS.

 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 easily switch between different NodeJS versions and install multiple versions simultaneously without conflicts. This flexibility is crucial, especially when adhering to a NodeJS job description that may require compatibility with multiple NodeJS versions.

It's important to note that NVM is different from package managers like NPM, Yarn, or PNPM, which are designed to manage packages and dependencies within a specific version of NodeJS.

Common questions

Was NodeJS the first to make Javascript run outside the browser?

No, there were many other alternatives to NodeJS or other ways of running Javascript on the server side, such as ASP, JSP, Spidermonkey and Rhino Javascript Engine, however, they became obsolete or failed to solve developer problems.

Are there alternatives available to NVM?

Yes, there are 4 best alternatives to NVM which are: –

NVS : Provides efficient and automatic version switching with a consistent, easy-to-use command-line interface
Nodista : A good choice for Windows users
N : A lightweight, 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 versions of NodeJS on a single system.

How do I configure NVM on Windows?

NVM is not available for Windows systems, however, there is a program called nvm-windows that you can download and access its README to start changing NodeJS versions on your Windows PC.

If you liked this article, check out our other guides below:

  • Change Node Version: A Step-by-Step Guide
  • Node JS Cache: Increasing Performance and Efficiency
  • Unlock the power of Node.JS microservices
  • Unlocking the Power of Websocket Nodejs
  • Best Text Editors and Node JS IDE for App Development

Source: BairesDev

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.