How to install Yarn on MacOs?

Yarn is a popular package manager used for JavaScript projects. It offers several advantages over other package managers such as npm.

When we checkout a source code that uses Javascript (be it nodejs or reactjs or package managed sources via npm) we may get the error: zsh: command not found: yarn, yarn command not found,.. when running yarn install. This error arises because we do not have Yarn installed on our computer

In this article, we will look at three ways to install Yarn on macOS.

Method 1: Install Yarn using Homebrew

Homebrew is a popular package manager on macOS. It makes it easy to install various applications and tools. To install Yarn using Homebrew, you need to follow these steps:

Install Homebrew if it is not already installed on your system. You can do this by opening Terminal and running the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install Yarn using the following command:

brew install yarn

Verify the version of Yarn installed using the following command:

yarn --version

If the above command returns the version of Yarn, then you have successfully installed Yarn using Homebrew.

Method 2: Install Yarn using npm

If you have already installed Node.js and npm on your system, you can install Yarn using npm by running the following command in Terminal:

npm install -g yarn

During installation if you get error like below:

npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/yarn'
npm ERR! }

You should install as root:

#install as root
sudo npm install -g yarn

After the installation is complete, you can verify the version of Yarn using the following command:

yarn --version

Method 3: Install Yarn using MacPorts

MacPorts is an open-source package manager for macOS that allows users to install applications and libraries from source code. To install Yarn using MacPorts, you need to follow these steps:

Install MacPorts if it is not already installed on your system. You can download MacPorts from https://www.macports.org/install.php.

After installation, open Terminal and update the list of ports by running the following command:

sudo port selfupdate

Install Yarn using the following command:

sudo port install yarn

Verify the version of Yarn installed using the following command:

yarn --version

 

If the above command returns the version of Yarn, then you have successfully installed Yarn using MacPorts.

Conclusion

In this article, we have looked at three ways to install Yarn on macOS using Homebrew, npm, and MacPorts. Installing Yarn will help you manage packages in your JavaScript projects more easily and efficiently. Depending on your preferences and requirements, you can choose the installation method that suits you best.

About Yarn

Yarn is a popular package manager used for managing dependencies in JavaScript projects. It was created by Facebook as a replacement for npm and has become the preferred choice for many developers due to its advantages over npm.

One of the key advantages of Yarn is its speed. Yarn has a faster installation process compared to npm as it caches downloaded packages, which makes subsequent installations much faster. It also supports parallel installation of packages, which further improves installation speed.

Another advantage of Yarn is its reliability. Yarn uses a lockfile called yarn.lock, which ensures that the same versions of dependencies are installed across all systems. This eliminates the risk of different versions of packages being installed on different machines, which can lead to compatibility issues.

Yarn also has a more robust dependency management system compared to npm. It uses a concept called “resolutions” that allows developers to specify which version of a dependency should be installed, even if it conflicts with another dependency. This helps to avoid conflicts and ensures that the correct version of each dependency is installed.

Additionally, Yarn offers a range of other useful features such as workspaces, which allow developers to manage multiple packages in a single repository, and offline caching, which allows developers to work on projects without an internet connection.

Related posts:

  1. How to Install Composer on MacOS Ventura: 3 Different Ways
  2. How to install LEMP on macOS Ventunra
  3. How to install imagick extension on MacOs?