Table of Contents
YARN vs NPM
NPM has few drawbacks like network connectivity issues, slow installation process to mention few and YARN was developed by Facebook to resolve those shortcomings.
YARN is not a replacement for NPM but in fact, it uses the same modules from NPM registry but with the different installation method. Since Yarn doesn’t require you to make any changes to the current workflow, it is possible to install YARN and use it at any point of time during your projects development phase.
Let’s get started with the installation of Yarn in an Ubuntu 18.04 system via the official Yarn APT package repository.
- You can open a SSH session in your Ubuntu 18.04 system using
Add Yarn Repository
The first step of adding a YARN repository is to import the GPG key of the said repository using the following CURL command:
Copy# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Now add the YARN repository in your Ubuntu 18.04 system by issuing the following command:
Copy# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Installing YARN on Ubuntu
Now that YARN repository has been added to your system, update the APT database and install YARN with the following commands:
Copy# apt update # apt install yarn
Remember, the above command will install NodeJS along with YARN. If you have already installed NodeJS using nvm (Node Version Manager) then you can skip installation of Nodejs using the following command:
Copy# apt install --no-install-recommends yarn
At this point, YARN will be installed in your Ubuntu 18.04 system. Check the version of installed YARN with:
Copy# yarn --version
Usages of YARN
1. YARN help
To find the list of available commands and flags along with a brief explanation on what it does, run the following YARN command from the terminal:
Copy# yarn help
Once you have successfully installed YARN, use it to create your own project, add dependencies to your project and more. Let us explore a few common usages of YARN.
2. Create a New Project
Create a new YARN project by using yarn init followed by the project name.
Copy# yarn init your_yarn_project
The above yarn command will prompt you to answer a few questions. You can answer them as per your choice or safely skip them by pressing Enter key. Once done, the yarn init command will create the package.json file containing the answers you have provided earlier. You can also manually edit the information in the package.json file at a later stage.
3. Add dependencies
Sometimes you need to add packages in your yarn project to fulfill the project dependencies. To do that, use the yarn add command followed by package name.
Copy# yarn add [package_name]
It is also possible to add specific version of the package with yarn add command by appending package version number or tag:
Copy# yarn add [package_name]@[tag_or_version]
4. Remove dependencies
If you want to remove an installed package, just use the
yarn remove command followed by the package name.
Copy# yarn remove [package_name]
5. Install all dependencies
To install all dependencies specified in the package.json file, just use yarn or yarn install. This is useful in situation when you have manually added the dependencies in package.json file and subsequently, want to install them.
Copy# yarn install OR # yarn
That’s it! You have now installed YARN in your Ubuntu 18.04 system and learned a few commonly used yarn command. To know more about yarn usages, visit the official documentation of yarn.