Yarn Package Manager
Yarn is a package manager for Node.js, similar to npm. While sharing a lot of common ground, there are some key differences between Yarn and npm.
Creating a basic package
Section titled “Creating a basic package”The yarn init command will walk you through the creation of a package.json file to configure some information about your package. This is similar to the npm init command in npm.
Create and navigate to a new directory to hold your package, and then run yarn init
mkdir my-package && cd my-packageyarn initAnswer the questions that follow in the CLI
question name (my-package): my-packagequestion version (1.0.0):question description: A test packagequestion entry point (index.js):question repository url:question author: StackOverflow Documentationquestion license (MIT):success Saved package.json✨ Done in 27.31s.This will generate a package.json file similar to the following
{ "name": "my-package", "version": "1.0.0", "description": "A test package", "main": "index.js", "author": "StackOverflow Documentation", "license": "MIT"}Now lets try adding a dependency. The basic syntax for this is yarn add [package-name]
Run the following to install ExpressJS
yarn add express
This will add a dependencies section to your package.json, and add ExpressJS
"dependencies": { "express": "^4.15.2"}Yarn Installation
Section titled “Yarn Installation”This example explains the different methods to install Yarn for your OS.
Homebrew
Section titled “Homebrew”brew updatebrew install yarnMacPorts
Section titled “MacPorts”sudo port install yarnAdding Yarn to your PATH
Section titled “Adding Yarn to your PATH”Add the following to your preferred shell profile (.profile, .bashrc, .zshrc etc)
export PATH="$PATH:`yarn global bin`"Windows
Section titled “Windows”Installer
Section titled “Installer”First, install Node.js if it is not already installed.
Download the Yarn installer as an .msi from the Yarn website.
Chocolatey
Section titled “Chocolatey”choco install yarnDebian / Ubuntu
Section titled “Debian / Ubuntu”Ensure Node.js is installed for your distro, or run the following
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -sudo apt-get install -y nodejsConfigure the YarnPkg repository
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.listInstall Yarn
sudo apt-get update && sudo apt-get install yarnCentOS / Fedora / RHEL
Section titled “CentOS / Fedora / RHEL”Install Node.js if not already installed
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -Install Yarn
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.reposudo yum install yarnInstall Yarn via AUR.
Example using yaourt:
yaourt -S yarnsudo eopkg install yarnAll Distributions
Section titled “All Distributions”Add the following to your preferred shell profile (.profile, .bashrc, .zshrc etc)
export PATH="$PATH:`yarn global bin`"Alternative Method of Installation
Section titled “Alternative Method of Installation”Shell script
Section titled “Shell script”curl -o- -L https://yarnpkg.com/install.sh | bashor specify a version to install
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version [version]Tarball
Section titled “Tarball”cd /optwget https://yarnpkg.com/latest.tar.gztar zvxf latest.tar.gzIf you already have npm installed, simply run
npm install -g yarnPost Install
Section titled “Post Install”Check the installed version of Yarn by running
yarn --versionInstall package with Yarn
Section titled “Install package with Yarn”Yarn uses the same registry that npm does. That means that every package that is a available on npm is the same on Yarn.
To install a package, run yarn add package.
If you need a specific version of the package, you can use yarn add package@version.
If the version you need to install has been tagged, you can use yarn add package@tag.