DEV Community

Cover image for How to install Node.JS: Secrets and Best Practices for Every Platform Revealed
brunohafonso
brunohafonso

Posted on

How to install Node.JS: Secrets and Best Practices for Every Platform Revealed

Overview

Node works on many platforms, there are a lot of ways to install it,
However, there are some practices we must follow while installing Node on Windows, Linux, and MacOS machines.

Learning Objectives

At the final of this article you will be able to:

  • Discover the best way to install and use Node in each platform.
  • Understand what executables are installed
  • Manage multiple Node versions
  • How to set a default Node version on your Machine.

Best practices to install Node on different platforms

The most common way to install Node is by using an OS package manager but this is not the best way to do it due to the reasons below:

  • OS package manager tends to lag behind the faster Node.JS release cycle (the latest versions cannot be available on the OS package manager)
  • The placement of binaries, folders, and files isn't standardized across OS package managers which can cause incompatibility issues.
  • Installing Node using a package manager requires the use of sudo on Non-Windows systems when we want to install a global package using the npm, and it's a critical security issue because we grant root privileges to the install process of third-party libraries.

We can also install Node directly from the Node.JS website but, Again a Non-Windows system requires the root privileges for installing global packages.

Installing NVM (Non-Windows systems)

The recommended way to install Node in Non-windows systems is by using a Node Version Manager, in particular nvm.

To install NVM we must use the install script available on Github, if you need to use a newer version you just need to change the version in the URL.

If you have curl installed already (it usually is) run the command bellow:

curl -o- htt‌ps://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

If using zsh (e.g., on newer macOS releases) the bash part of the command can be replaced with zsh.

You can download and execute the file as well:

cat install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Again you can replace bash by zsh

To check if the installation was successful use the following command:

command -v nvm
Enter fullscreen mode Exit fullscreen mode

It should output nvm.

If this fails on Linux, close and reopen the terminal and try running the command again.

On macOS see GitHub for in-depth troubleshooting instructions.

Now that you have the nvm installed, go ahead and install a Node version by using this command:

nvm install 20
Enter fullscreen mode Exit fullscreen mode

This command will install a Node version respecting the major version but, minor and patches will change, if you want to install a specific version you must provide the entire version number.

You can install any version you want using this command above.
By default the nvm will set the first Node version you installed as the default, but you can override the default version using the command nvm alias default 20

To check if the installation was successful run this command:

node -v
Enter fullscreen mode Exit fullscreen mode
npm -v
Enter fullscreen mode Exit fullscreen mode

And that's it now you have the Node installed on your machine following the best practices.

Installing NVS (Windows systems)

While we have nvm for Linux and MacOS, and an unaffiliated nvm-windows version manager, the recommended version manager for Windows is nvs, it is cross-platform and can be used on Linux and MacOS, but on these systems the most common is nvm.

To install nvs use the following command:

winget install jasongin.nvs
Enter fullscreen mode Exit fullscreen mode

Or using chocolatey

choco install nvs
Enter fullscreen mode Exit fullscreen mode

You can visit the nvs releases page, and download the msi `file of the last release to install it.

On the first time running nvs, the command may ask for agreement to terms.

Once installed, install the latest version 20 release:

bash
nvs add 20

then execute the command below to select the installed version

bash
nvs use 20

You can install any version you want using the commands above.
By default the nvs doesn't set a Node version as default, but you can set the default version using the command nvs link 20

To confirm if Node was installed successfully use this command:

bash
node -v

bash
npm -v

And that's it now you have the Node installed on your machine following the best practices.

Which binaries are included with Node ?

The Node installation includes the npm installation which is the Node package manager that we use to manage the third-party libraries that we will use on the projects or as a global package on our machines.

Top comments (0)