Yarn is a new package manager for JavaScript and the primary tool used by Facebook to manage its source code. As of this writing, Yarn has approximately 370 contributors and is used by notable tech companies such as Airbnb, Dropbox, Uber, Google, and Netflix. If you’re reading this article, it means that you’re probably interested in installing Yarn on your Mac so you can use it to manage your personal projects or help teammates collaborate effectively at work.
In this tutorial, we’ll show you how to install Yarn on Mac OS in five simple steps.
Installing Yarn on macOS
Choose one of the below-given methods to install Yarn on the macOS system. All the methods are secure and easy to follow.
- Method 1 – Using Homebrew
You can install Yarn using the Homebrew package manager on the macOS system. Homebrew is a package manager for macOS operating system that provides an easier way to install and manage packages on your system.
To install Yarn on macOS, open a terminal and type:
brew install yarn
Make sure you have installed Homebrew on your macOS system.
- Method 2 – Using Shell Script
Another way to easily install Yarn on macOS. This is a shell script provided officially for installing Yarn on different Unix/Linux operating systems.
You can install Yarn by running the following code in your terminal:
curl -o- -L https://yarnpkg.com/install.sh | bash
The script will verify the GPG signature first before the installation. So it is also a secure way to install Yarn on macOS systems.
Check Yarn Version
Once the Yarn installation is completed, execute the following command to verify the yarn version.
yarn --version
1.22.4
Working with Yarn Package Manager
As of now, you have successfully installed Yarn on the macOS system. Let’s explore some basic uses of the Yarn command line.
- Create and initialize a new project
Use
yarn init
under a empty folder to create new project.yarn init
The command will go through an interactive session asking a few questions. Input the required values and press Enter. This will generate a
package.json
file in current directory.yarn init v1.22.4 question name (new_project): my-package question version (1.0.0): question description: Test package question entry point (index.js): question repository url: https://github.com/tecadmin/my-yarn-package question author: Rahul question license (MIT): question private: success Saved package.json Done in 84.90s.
- Adding a new module
Use
yarn add
command to install new dependency to your existing application. You need to specify the package name to add. It will also make a entry inpackage.json
as dependency.yarn add [package]
Also, you can specify the package version or tag to select the correct package version instead of the latest version.
yarn add [package]@[version]
yarn add [package]@[tag]
- Upgrading a dependency
Use the yarn upgrade command to upgrade already installed packages in your application.
yarn upgrade [package]
You can also specify a version or tag name to upgrade to specific versions.
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]
- Removing a dependency
If any of the packages is not more required, remove it from your application.
yarn remove [package]
- Using
yarn install
This will install all the packages dependencies defined in
package.json
file.yarn install
yarn install is the default action of the yarn command without passing any subcommand.
Conclusion
This tutorial explained to you to install yarn on the macOS system. Also explored the details to work with the yarn package manager.