Table of Contents
In this article, I will take you through 21 Practical YARN command examples to manage projects in Linux. Yarn is an independent cross platform, open source package manager developed by Facebook in 2016 for the Node.js JavaScript runtime environment. It is not just safe and stable but also gives a guarantee that an install that works will continue to work the same way in the future. Using yarn, users can write their own plugins which can be used by others. There are tons of other features yarn provides which can be checked in its official website. Here we will see practical yarn command examples in below section.
21 Practical YARN Command Examples to Manage Projects in Linux
Also Read: How to Install Clonezilla on Ubuntu 20.04 LTS (Focal Fossa)
Example 1: How to Check YARN command version
To check the current installed version of yarn utility, you need to use yarn --version
command as shown below.
root@cyberithub:~# yarn --version 1.22.19
Example 2: How to Initialize a YARN Project
You can use yarn init <package>
syntax to interactively create or update a package json file. In this example, we are creating package.json
file for test_project
using yarn init test_project
command as shown below.
root@cyberithub:~# yarn init test_project
yarn init v1.22.19
question name (root):
question version (1.0.0):
question description: Creating a Project
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
Done in 51.81s.
Example 3: How to add a Package in your Project using yarn command
To install a new package to your project, you need to use yarn add <package>
syntax. In this example, we are installing express
package of version 4.15.5
using yarn add express@4.15.5
command as shown below.
root@cyberithub:~# yarn add express@4.15.5
yarn add v1.22.19
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 32 new dependencies.
info Direct dependencies
└─ express@4.15.5
info All dependencies
├─ accepts@1.3.8
├─ array-flatten@1.1.1
├─ content-disposition@0.5.2
├─ content-type@1.0.4
├─ cookie-signature@1.0.6
├─ cookie@0.3.1
├─ depd@1.1.2
├─ destroy@1.0.4
├─ ee-first@1.1.1
├─ etag@1.8.1
├─ express@4.15.5
├─ finalhandler@1.0.6
├─ forwarded@0.1.2
├─ http-errors@1.6.3
├─ inherits@2.0.3
├─ ipaddr.js@1.4.0
├─ media-typer@0.3.0
├─ merge-descriptors@1.0.1
├─ methods@1.1.2
├─ mime-db@1.52.0
├─ mime-types@2.1.35
├─ mime@1.3.4
├─ negotiator@0.6.3
├─ path-to-regexp@0.1.7
├─ proxy-addr@1.1.5
├─ qs@6.5.0
├─ serve-static@1.12.6
├─ setprototypeof@1.0.3
├─ type-is@1.6.18
├─ unpipe@1.0.0
├─ utils-merge@1.0.0
└─ vary@1.1.2
Done in 3.11s.
Example 4: How to Upgrade a package using yarn command
To upgrade a package to the latest version, you need to use yarn upgrade <package>
syntax. In this example, we are upgrading express
package to the latest version by using yarn upgrade express
command as shown below.
root@cyberithub:~# yarn upgrade express
yarn upgrade v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Rebuilding all packages...
success Saved lockfile.
success Saved 32 new dependencies.
info Direct dependencies
└─ express@4.15.5
info All dependencies
├─ accepts@1.3.8
├─ array-flatten@1.1.1
├─ content-disposition@0.5.2
├─ content-type@1.0.4
├─ cookie-signature@1.0.6
├─ cookie@0.3.1
├─ depd@1.1.2
├─ destroy@1.0.4
├─ ee-first@1.1.1
├─ etag@1.8.1
├─ express@4.15.5
├─ finalhandler@1.0.6
├─ forwarded@0.1.2
├─ http-errors@1.6.3
├─ inherits@2.0.3
├─ ipaddr.js@1.4.0
├─ media-typer@0.3.0
├─ merge-descriptors@1.0.1
├─ methods@1.1.2
├─ mime-db@1.52.0
├─ mime-types@2.1.35
├─ mime@1.3.4
├─ negotiator@0.6.3
├─ path-to-regexp@0.1.7
├─ proxy-addr@1.1.5
├─ qs@6.5.0
├─ serve-static@1.12.6
├─ setprototypeof@1.0.3
├─ type-is@1.6.18
├─ unpipe@1.0.0
├─ utils-merge@1.0.0
└─ vary@1.1.2
Done in 1.53s.
Example 5: How to Verify Integrity of a Package using yarn command
To verify the integrity of a package, you need to use yarn check --integrity
command as shown below. This will verify that versions and hashed values of the package contents in the project’s package.json
match those in yarn’s lock file. This helps to verify that the package dependencies have not been altered.
root@cyberithub:~# yarn check --integrity yarn check v1.22.19 success Folder in sync. Done in 0.20s.
Example 6: How to List all the Packages and its dependencies
To list out all the packages and its dependencies, you need to use yarn list
command as shown below. This command will lists all the dependencies for the current working directory by referencing all package manager meta data files, which includes a project’s dependencies.
root@cyberithub:~# yarn list
yarn list v1.22.19
Done in 0.14s.
Example 7: How to Install Packages Globally in your System using yarn command
To install a package globally in the system, you need to use yarn global add <package>
syntax. In this example, we are installing nodemon package by using yarn global add nodemon
command as shown below.
root@cyberithub:~# yarn global add nodemon yarn global v1.22.19 [1/4] Resolving packages... [2/4] Fetching packages... [3/4] Linking dependencies... [4/4] Building fresh packages... success Installed "nodemon@2.0.16" with binaries: - nodemon Done in 5.91s.
Example 8: How to List all the Cached Packages using yarn command
By default yarn stores every package in a global cache in your user directory in a file system. To list all the packages from this cache, you need to use yarn cache list
command as shown below.
root@cyberithub:~# yarn cache list
yarn cache v1.22.19
Name Version Registry Resolved
@sindresorhus/is 0.14.0 npm https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.14.0.tgz#9fb3a3cf3132328151f353de4632e01e52102bea
@szmarczak/http-timer 1.1.2 npm https://registry.yarnpkg.com/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421
abbrev 1.1.1 npm https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8
accepts 1.3.8 npm https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e
ansi-align 3.0.1 npm https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.1.tgz#0cdf12e111ace773a86e9a1fad1225c43cb19a59
ansi-regex 5.0.1 npm https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304
ansi-styles 4.3.0 npm https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.3.0.tgz#edd803628ae71c04c85ae7a0906edad34b648937
Example 9: How to Manage YARN Configuration Files
If you want to check the current YARN configurations then you need to run yarn config list
command as shown below.
root@cyberithub:~# yarn config list
yarn config v1.22.19
info yarn config
{ 'version-tag-prefix':
'v',
'version-git-tag':
true,
'version-commit-hooks':
true,
'version-git-sign':
false,
'version-git-message':
'v%s',
'init-version':
'1.0.0',
'init-license':
'MIT',
'save-prefix':
'^',
'bin-links':
true,
'ignore-scripts':
false,
'ignore-optional':
false,
registry:
'https://registry.yarnpkg.com',
'strict-ssl':
true,
'user-agent':
'yarn/1.22.19 npm/? node/v10.19.0 linux x64',
lastUpdateCheck:
1652782561421 }
info npm config
{}
Done in 0.05s.
Similarly, if you want to check the value for any specific key say init-license
in this case, then you need to use yarn config get init-license
command as shown below.
root@cyberithub:~# yarn config get init-license MIT
Example 10: How to Link a Package to another Project using yarn command
To check the package you want to link, use yarn link
command as shown below.
root@cyberithub:~# yarn link
yarn link v1.22.19
success Registered "react".
info You can now run `yarn link "react"` in the projects where you want to use this package and it will be used instead.
Done in 0.08s.
Now to link react to another project, you need to run yarn link react
in that project as shown below.
root@cyberithub:~# yarn link react
yarn link v1.22.19
success Using linked package for "react".
Done in 0.10s.
Example 11: How to Check Information about a Package using yarn command
If you want to check complete information about a package then you need to use yarn info <package>
syntax. In this example, we are checking complete information about express package by using yarn info express
command as shown below.
root@cyberithub:~# yarn info express | more
yarn info v1.22.19
{ name:
'express',
description:
'Fast, unopinionated, minimalist web framework',
'dist-tags':
{ latest:
'4.18.1',
next:
'5.0.0-beta.1' },
versions:
[ '0.14.0',
'0.14.1',
'1.0.0-beta',
'1.0.0-beta2',
'1.0.0-rc',
'1.0.0-rc2',
'1.0.0-rc3',
'1.0.0-rc4',
'1.0.0',
'1.0.1',
'1.0.2',
................................
Example 12: How to Store Registry Username and Email
To store username and email into npm registry, you can use yarn login
command as shown below.
root@cyberithub:~# yarn login yarn login v1.22.19 question npm username: cyberithub question npm email: admin@cyberithub.com Done in 19.69s.
Example 13: How to Clear registry Username and Email using yarn command
If you want to remove your stored username and email from npm registry then you need to run yarn logout
command as shown below.
root@cyberithub:~# yarn logout yarn logout v1.22.19 success Cleared login credentials. Done in 0.05s.
Example 14: How to Check why a package is installed using yarn command
If you want to check why a specific package is installed then you need to use yarn why <package>
syntax. In this example, we are checking about jest package by using yarn why jest
command as shown below.
root@cyberithub:~# yarn why jest
yarn why v1.22.19
[1/4] Why do we have the module "jest"...?
[2/4] Initialising dependency graph...
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "jest@28.1.0"
info Has been hoisted to "jest"
info This module exists because it's specified in "dependencies".
info Disk size without dependencies: "48KB"
info Disk size with unique dependencies: "460KB"
info Disk size with transitive dependencies: "25.64MB"
info Number of shared dependencies: 11
Done in 0.68s.
Example 15: How to Install all the dependencies for a Project using yarn command
To install all the dependencies for a project, you need to use yarn install
command as shown below.
root@cyberithub:~# yarn install
yarn install v1.22.19
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.32s.
Example 16: How to Check all the Package Owners using yarn command
To check all the owners of a package, you need to use yarn owner list <package>
syntax. In this example, we are checking the ownership of express package by using yarn owner list express
command as shown below.
root@cyberithub:~# yarn owner list express
yarn owner v1.22.19
[1/1] Getting owners for package "express"...
info mikeal <mikeal.rogers@gmail.com>
info dougwilson <doug@somethingdoug.com>
info jasnell <jasnell@gmail.com>
Done in 0.41s.
Example 17: How to Define a Policy for your Project using yarn command
If multiple people are working on a project, then there is a chance that all of them are working on a different yarn version. So to avoid any potential breaking of project, it is a good idea to set the version so that everyone uses the same one. This can be easily done by using yarn policies set-version
command as shown below.
root@cyberithub:~# yarn policies set-version
Warning: Your current Yarn binary is currently Yarn 1.22.19; to avoid potential breaking changes, 'set version latest' won't receive upgrades past the 1.22.x branch.
To upgrade to the latest versions, run yarn set version stable instead. Sorry for the inconvenience.
Resolving latest to a url...
Downloading https://github.com/yarnpkg/yarn/releases/download/v1.22.19/yarn-1.22.19.js...
Saving it into /root/.yarn/releases/yarn-1.22.19.cjs...
Updating /root/.yarnrc...
Done!
Example 18: How to Check outdated Package dependencies using yarn command
If you want to check all the outdated package dependencies for a project then you need to use yarn outdated
command as shown below. In our case, we don't have any outdated package dependencies so nothing shows on the output but in general the output includes the currently installed version, the desired version based on semver, and the latest available version.
root@cyberithub:~# yarn outdated
yarn outdated v1.22.19
Done in 0.44s.
Example 19: How to Remove unnecessary files from package dependencies
If you want to remove all the unnecessary files from package dependencies then you need to use yarn autoclean
command as shown below.
root@cyberithub:~# yarn autoclean
yarn autoclean v1.22.19
info ".yarnclean" does not exist. Autoclean will delete files specified by ".yarnclean". Run "autoclean --init" to create ".yarnclean" with the default entries.
Done in 0.04s.
If you are running yarn autoclean command for the first time, then you need to run with --init
switch to generate .yarnclean
with the default entries.
root@cyberithub:~# yarn autoclean --init
yarn autoclean v1.22.19
[1/1] Creating ".yarnclean"...
info Created ".yarnclean". Please review the contents of this file then run "yarn autoclean --force" to perform a clean.
Done in 0.04s.
Example 20: How to Publish a Package to the npm registry using yarn command
If you want to publish a package to the npm registry then you need to use yarn publish
command as shown below.
root@cyberithub:~# yarn publish yarn publish v1.22.19 [1/4] Bumping version... info Current version: 1.0.0 question New version: [2/4] Logging in... question npm username: cyberithub question npm email: admin@cyberithub.com question npm password:
Example 21: How to List all the options available with yarn command
If you want to list all the options available with yarn command then you need to use yarn help
command as shown below.
root@cyberithub:~# yarn help
Usage: yarn-1.22.19.cjs [command] [flags]
Displays help information.
Options:
--cache-folder <path> specify a custom folder that must be used to store the yarn cache
--check-files install will verify file tree of packages for consistency
--cwd <cwd> working directory to use (default: /root)
--disable-pnp disable the Plug'n'Play installation
--emoji [bool] enable emoji in output (default: false)
--enable-pnp, --pnp enable the Plug'n'Play installation
--flat only allow one version of a package
--focus Focus on a single workspace by installing remote copies of its sibling workspaces.
--force install and build packages even if they were built before, overwrite lockfile
--frozen-lockfile don't generate a lockfile and fail if an update is needed
--global-folder <path> specify a custom folder to store global packages
--har save HAR output of network traffic
--https-proxy <host>