yarn was created in order to overcome some of the problems faced in dealing with packages for large codebases by Facebook in collaboration with Google and a few other companies.
Yarn stands for Yet Another Resource Negotiator.
How to install yarn?
Yarn can be installed in various methods based on your operating system. With yarn version 2, a lot of things have changed compared to v1. But as of now installing process remains the same and to even use v2, we need to first install the latest v1.
Install yarn using npm
Yes, using npm we can install yarn as a global package, for this, you need to have npm already installed. This works for all types of operating systems.
npm install -g yarn
If you face any issues with installation or running yarn installed using npm, then you can follow up the OS-specific method for installing yarn.
Yarn installation for Mac users
Mac users can use homebrew for installing the yarn package manager.
brew install yarn
For other ways of installation, you can visit this yarn official link and under alternatives dropdown select macOS and you'll see different ways of installing yarn in mac.
Yarn installation for Windows users
Windows users can download the latest yarn
.msi file from this link and run it for installing the yarn. By using chocolatey and scoop package managers also yarn can be installed.
After the installation is completed you can confirm it by running the below command
If you see the version number without any errors then you've successfully installed yarn.
yarn is a CLI tool using which we can manage the packages from the npm registry.
Let's see the different yarn commands to manage npmjs packages.
There are a lot of yarn commands for dealing with npm packages but our main focus will be on the commands related to consuming the packages not creating or publishing those.
Note: We'll be seeing all commands from yarn v1 and a few updated commands of v2.
Essential yarn commands
To check the yarn version
yarn --version(or -v) // To see your yarn version
yarn init or yarn init -y // To autocomplete the details
For installing packages
yarn add <package> // To save in dependencies list yarn add --dev(or -D) <package> // To save in dev dependencies list // To install a particular version of the package yarn add <package>@2.1.1 // version as per the need, 2.1.1 is a sample version // To install all the packages from package.json yarn or yarn install // To install a global package accessible to all the projects yarn global add <package>
For listing or checking packages
yarn list // By default lists all the packages and their dependencies yarn list --depth=0 // To restrict the depth of the dependencies use depth flag // To list global packages just use global before list command yarn global list // Lists all globally installed packages and their dependencies yarn global list --depth=0 // Lists all global packages with zero level dependencies
For updating packages
// For updating a single package (semver contraints will be respected) yarn upgrade <package> // Updates package to the latest version in the current project yarn global upgrade <package> // Updates global package to the latest version // For updating all packages yarn upgrade // Updates all packages to their latest versions respecting their semver constraints yarn global upgrade // Updates all global packages to their latest versions respecting semver constraints // To check the outdated packages instead of directly updating yarn outdated // Lists the outdated packages in the current project directory yarn upgrade-interactive // Before updating the packages it shows a list of outdated packages to select // To manually update any package to any version yarn upgrade <package>@version yarn global upgrade <package>@version
Semver means semantic versioning. For the dependencies in the package.json, besides version number, a small hat(
^) or tilde(
~) is prefixed for some dependencies. It means those dependencies can be updated to the latest minor version when it is prefixed with (
^) and to the latest patch version when it is prefixed with (
yarn upgrade command when updating such dependencies considers semver prefixes without breaking the project or that dependency.
There's a whole site dedicated for semver to learn more about it.
For uninstalling packages
yarn remove <package> // To uninstall the package installed locally yarn global remove <package> // To uninstall the globally installed package
For getting package details
yarn info <package> // Shows details about that particular package yarn info <package>@version // To see specific package version details // To see the details of the package it need not be installed locally
For auditing packages
yarn audit // To scan all dependencies and list all the vulnerabilities yarn audit --json // To audit and list the details in a JSON format
yarn run <command> // Any script/command listed in the scripts in package.json yarn run // Lists all the scripts available to run yarn import // To generate yarn.lock from an npm package-lock.json while migration
Note all the commands are as per yarn v1 and in v2 lot of new commands are added. Once everyone starts using v2, these commands will be updated.
So that is all about the most important yarn commands to know for getting started using the yarn package manager.