Yarn is a package manager for for node.js applications. It allows you to create new packages (peace of code to do specific task) and share with the community.
It provides a command line interface to easy to install, update and manage packages for a Node application. This tutorial will help you to install yarn on macOS systems.
Install Yarn on macOS
Choose one of the below given methods to install Yarn on 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 macOS system. Homebrew is an package manager for macOS operating system provides 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 pon 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 secure way to install Yarn on macOS systems.
Check Yarn Version
Once the Yarn installation completed, execute the following command to verify yarn version.
As of now, your have successfully install Yarn on macOS system. Let’s explore some basic uses of Yarn command line.
1. Starting a new project
yarn init under a empty folder to create new project.
The command will go through an interactive session asking 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.
2. Adding a dependency
yarn add command to install new dependency to your existing application. You need to specify package name to add. It will also make a entry in
package.json as dependency.
yarn add [package]
Also you can specify package version or tag to select correct package version instead of latest version.
yarn add [package]@[version]
yarn add [package]@[tag]
3. Upgrading a dependency
Use yarn upgrade command to upgrade already installed packages in your application.
yarn upgrade [package]
You can also specify version or tag name to upgrade to specific versions.
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]
4. Removing a dependency
If any of package is not more required, remove it from your application.
yarn remove [package]
5. Installing dependencies
This will install all the packages dependencies defined in
yarn install is the default action of yarn command without passing any subcommand.
This tutorial explained you to installing yarn on macOS system. Also explored the details to work with yarn package manager.