. Advertisement .
..3..
. Advertisement .
..4..
It is fairly easy to fix the “yarn: command not found” error. Follow this tutorial, but make sure you have backed up your important files first.
Solutions For The “yarn: command not found” Error
Why The Error Happens
You will see this error message when your shell can’t find the executable file of the yarn command.
Whenever you enter a command into a console, the shell will run through PATH – a predefined list of directories in the system. They are supposed to host all the executables installed. The “yarn: command not found” error message is produced when the shell can’t locate any binary file named yarn in those paths.
Some possible causes:
- You have not installed yarn in your system.
- Your PATH environment variable doesn’t contain the directory that has the yarn executable.
Install yarn
It doesn’t matter if you installed the yarn module before. Perhaps you have accidentally removed it along with other modules, or its files have been corrupted.
Reinstallation can reset its state and place every file where it is supposed to be.
The yarn project officially recommends npm as your package manager, which is available if you have already installed Node.js.
On Linux/macOS:
$ sudo npm install -g yarn
After the installation, you should call the yarn command without any issue. For instance, check the version of your yarn module by using this command:
$ yarn --version
On Windows, go to the yarn’s official website and download the MSI installer (https://classic.yarnpkg.com/latest.msi). Run the .msi file to install yarn.
If you have Chocolatey on Windows, run this command:
choco install yarn
Run this command in your terminal app to verify the installation:
yarn --version
Fix The PATH Environment Variable
Remember that npm manages modules in your system, including every file of each module. When you install a module, there are two modes: local and global installation.
npm will place files of the module in different locations depending on this mode. By default, local installation puts files right in your home folder, while a globally installed module ends up in the /usr/local or %AppData%\Roaming\npm, depending on your operating system.
Modules are installed to the lib/node_modules directory in this “prefix” directory (e.g. /usr/local/lib/node_modules).
You can find out the location of this directory (of both scopes) by using the npm root command.
$ npm root
$ npm root -g
A typical Linux distribution or macOS will print out /usr/local/lib/node_modules while this location on Windows is C:\Users\<your_usernam>\AppData\Roaming\npm\node_modules.
In those cases, /usr/local or %AppData%\Roaming\npm is your npm installation directory and determines the location of modules through the prefix config.
In particular, they are installed in {prefix}/bin on Linux/macOS or directly in {prefix} on a Windows system (in which prefix defaults to the folder in which npm is installed).
You can solve the “yarn: command not found” error by adding it into your PATH environment variable (just how you can deal with the same “npm: command not found” error).
On Linux/macOS:
$ export PATH=$PATH:$HOME/npm_modules:/usr/local/bin
You can also add it permanently to the PATH variable by adding this line to your .bashrc file:
PATH=$PATH:$HOME/npm_modules:/usr/local/bin
On Windows, follow the npm: command not found error tutorial and add the installation directory of npm to your system’s PATH variable.
Conclusion
When the “yarn: command not found” error occurs, you may need to reinstall yarn or adjust the PATH environment variable. These methods make sure the shell of your system can locate and execute the yarn binary file.
Leave a comment