Solution to the error "ERROR: [Errno 2] No such file or directory" in Node.js.

A very common error in Node.js occurs when we try to install or create a new project and the system is unable to execute the action. This usually happens because we have installed Yarn from the official repositories of our distro since these versions are usually outdated.
The generalized error is as follows:
ERROR: [Errno 2] No such file or directory
If you are trying to install dependencies for an existing project, your error will look exactly like this:
ERROR: [Errno 2] No such file or directory: 'install'
If you are trying to create a new project, it will look like this:
ERROR: [Errno 2] No such file or directory: 'create'
Whatever the case may be, the error is derived from the same problem.
Solution
We can solve this error by installing yarn again from a different source. I recommend installing it with NPM since we will get the latest version of yarn and we can do it easily no matter what our Linux distro is.
First of all, you must uninstall the particular version of yarn from your operating system:
Ubuntu / Debiansudo apt-get remove --purge yarn cmdtest
Arch Linux
sudo pacman -Rsn yarn
CentOS
sudo yum remove yarn && sudo rm -rf /etc/yarn
Rocky Linux
sudo dnf remove yarn && sudo rm -rf /etc/yarn
To install yarn with npm you can run the following command:
npm install --global yarn
After the installation you will need to close your current terminal session and open a new one to use the yarn command.
If you do not have npm installed, I recommend following the following tutorial to install it using nvm:
- Installation of npm and Nodejs with nvm
For a specific operating system:
- Installing the latest version of npm and Nodejs on Ubuntu
- Installing the latest version of npm and Nodejs on GNU/Linux Debian
- Installing the latest version of npm and Nodejs on Arch Linux