w3resource

Yarn usage: Commands and use Cases


In the previous tutorial we showed you how to install yarn on different operating systems. If you have not installed Yarn, please read that tutorial to find out the best way to install Yarn for your operating system.

Having installed Yarn, you can now start using Yarn. This tutorial will present to you some of the use cases for Yarn and the commands you will use during those cases.

Starting a new project

To start a new project, you need to run:
yarn init
.

Adding a dependency

You can use any of these formats to add a dependency to your project.

yarn add [package]```
```yarn add [package]@[version]```
```yarn add [package]@[tag]

How to add a dependency to different categories of dependencies

To add to devDependencies, peerDependencies, and optionalDependencies, run:

yarn add [package] --dev```
````yarn add [package] --peer```
```yarn add [package] --optional

respectively.

Upgrading a dependency

If you need to upgrade a dependency, you can run any of the commands below:

yarn upgrade [package]```
````yarn upgrade [package]@[version]```
```yarn upgrade [package]@[tag]

Remove a dependency

To remove a dependency that you are no longer using, run

yarn remove [package]

Installing all the dependencies of a project

Every project that is created using the yarn init command has a package.json file. This is where yarn stores information of all the dependencies required for a project, to install these dependencies, you can either run:

yarn

or you run:

yarn install

Previous: Comprehensive Guide to Yarn Installation on Various OS.
Next: Yarn cli commands



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/yarn/yarn-usage.php