Dskr.dev

Русский

18 Sep 2023

Edit

How to install the node.js environment properly?

Install Node.JS

There are several ways to install Node.JS and it can be difficult to determine which one is best. In this article we will compare the different methods (direct installation, system package managers, nvm, n, nodenv, volta, fnm). TLDR: I prefer to use fnm

The easiest option for installing Node.JS is to visit nodejs.org and download the installer. However, this may not be the best approach. A slightly more reliable option is to use a system package manager (brew, apt, dnf). With either method, it is not possible to change the Node.JS version quickly. Often a current project requires a fresh LTS version, a legacy project is running under a previous LTS version. Additionally, if you want to experiment with the latest features, it may be necessary to upgrade to the current version of Node.js.

The most popular solution is Node Version Manager nvm. However, it has some drawbacks. First of all, nvm can be incredibly slow, as each time you open the console with it installed, it can take up to several seconds to load. Secondly, nvm does not work with fish and Windows. Thirdly, if you use globally installed packages, you will need to install them anew every time or register them in a special file. n and nodenv have similar issues.

The next solution is Volta. It is written in Rust, which means it should be fast. Supports fish and even windows. Globally installed libraries work even after a version change. But the .nvmrc/.node-version files are not supported, although it is suggested to write the node version in the package' as a replacement.json. Another disadvantage is that pnpm support is in experimental mode.

And finally Fast Node Manager fnm. It is also written in Rust. It works fast and supports everything you need. fnm is installed with one command:

curl -fsSL https://fnm.vercel.app/install | bash

Next, you can install the Node version.JS to be used by default:

fnm default 18

Now you can add the .node-version file to the project, specifying the Node version.The JS that will be used in this project.

echo 18.17.0 > .node-version

When you open the console in the project folder, fnm will automatically enable the desired version Node.JS .

There is a slight disadvantage if the Node version is specified in the project.JS that is not installed, it will not be installed automatically, but an error will be displayed when opening the console. To install the desired version, you will need to enter as many as two commands

fnm install
fnm use

Install pnpm/yarn

Node.JS, starting with version 16.13, now supports Corepack. This utility allows you to use any supported package manager without manual installation. All you need to do is enable corepack:

corepack enable

After that, when using pnpm/yarn for the first time, they will automatically install and work.