Yarn is a super simple dependency management tool which is way faster to use instead of traditional npm. It acts as a drop-in replacement, so you can get started using yarn right away. The best way to install yarn is by using npm. That’s right, you use npm to install yarn, npm’s replacement essentially, how fun is that!
There are a few great reasons as to why you should start using yarn today.
- Yarn sets up a lock file with the exact versions being used within a project. This improves the speed of installation dramatically and also allows you to share your lock file with other developers to ensure that everyone involved is using the same version numbers of a given package.
- Yarn also caches every package it downloads locally, this way you never need to download the same package twice.
- Yarn also installs packages in parallel improving the speed in which projects are set up.
- Yarn also uses checksums to install packages, making it super secure and validating the packages before any code is run within your codebase.
All of the following installation instructions are available on the yarn documentation. I have included the information below for ease of reference.
My preferred method if you have NPM already installed on your system, you could simply run the following command to get yarn easily installed.
npm install --global yarn
There are plenty of other options to get it installed. I have included the link below to the yarn documentation for you to pick the option that works best for you.
Usage within a project
Now that we have everything installed, we need to start configuring our project to use yarn. Once within your project root directory, you’ll want to run the following command.
If you already have a yarn.lock file within your project, or if you have a package.json file you can run the following command.
yarn install or
Adding new packages/modules
Installing packages is super simple now that you have yarn configured. The quickest and easiest way is by using the following.
yarn add [package]
If you want to add a specific version to your project instead of using the latest version of a package you’d run the following.
yarn add [package]@[version]
You can also specify a specific tagged version of a package by running the following command.
yarn add [package]@[tag]
Deleting a Package
Deleting/Removing a package could not be any easier. Simply run the following command and yarn will take care of the rest for you.
yarn remove [package]
Updating/Upgrading a Package
To upgrade an existing package to the latest version you’d just need to run the following command.
yarn upgrade [package]
You can also specify what version to update to by running the following command.
yarn upgrade [package]@[version]
If you want to upgrade a specific tagged version, then you could run the following as well.
yarn upgrade [package]@[tag]