What is Yarn?
Yarn is an alternative npm-client used for working with JavaScript as a package manager that was developed by the Facebook team. It also allows us to share code with other developers around the world and use solutions from other developers. The code itself is distributed via packages, sometimes referred to as modules. The package contains a .package.json file that describes the contents, as well as all the associated distributed code. In comparison with npm, Yarn is considered to be a better alternative and surpasses NPM in the following parameters:
- Speed compared to the standard client, while there are comparisons and testing where Yarn was two to three times faster.
- Reliability of installation. The reliability is better for two reasons:
- Short-term computer network problems can cause a failure in receiving packets.
- When new versions of packages are released, there may be changes that are incompatible with other packages that could disrupt the application. Yarn addresses these two problems and solves them easily.
- Yarn is also compatible with Npm and Bower.
Prerequisites
Node.js must be installed before installing Yarn. We can verify if node.js is installed on our system using the Windows terminal or PowerShell.
1. Open the terminal by pressing the Win+R buttons and then enter cmd.
2. Using the command line, we can verify if Node.js is installed with the command:
C:\Windows\system32>node -v‘
node’ is not recognized as an internal or external command, operable program or batch file.
C:\Windows\system32>
If we do not have node.js installed, we can go to the main node.js website and download the installer.
Next, we need to run the installer.
Now, we should accept the license agreement.
Now, leave all the defaults set and click Next to install node.js.
Finally, click on Finish, to complete the installation. Now we can rerun our command to verify node.js installed correctly using the following command in the terminal:
C:\Windows\system32>node --version
V14.3.0
C:\Windows\system32
Yarn Installation
MSI Installation
The first method we will use to install Yarn is to Download the Yarn.msi installer from the official Yarn website and install it on our local computer.
Next, we will run the .msi installer.
Now, accept the license agreement.
Then, click Install.
And finally, click on the Finish button.
Now, we can verify Yarn was installed using the following CLI commands.
C:\Windows\system32>yarn --version
1.22.4
C:\Windows\system32>
Chocolatey Installation
Another way to install Yarn is by using the Chocolatey package manager for Windows. This method is convenient because Chocolatey will determine whether node.js is already installed and if not, it will automatically install it for us. It is important to do everything as an administrator. Here are the instructions for installing Chocolatey.
Once Chocolatey is set up, we can install Yarn using the following command.
C:\Windows\system32>choco install yarn
Chocolatey v0.10.15
Installing the following packages:
yarn
By installing you accept licenses for the packages.
yarn v1.22.4 [Approved]
yarn package files install completed. Performing other installation steps.
The package yarn wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): Y
Downloading yarn
from 'https://yarnpkg.com/downloads/1.22.4/yarn-1.22.4.msi'
Progress: 100% - Completed download of C:\Users\Dmitry\AppData\Local\Temp\chocolatey\yarn\1.22.4\yarn-1.22.4.msi (1.57 MB).
Download of yarn-1.22.4.msi (1.57 MB) completed.
Hashes match.
Installing yarn...
yarn has been installed.
#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><Obj S="progress" RefId="1"><TNRef RefId="0" /><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><S S="debug">Host version is 5.1.18362.752, PowerShell Version is '5.1.18362.752' and CLR Version is '4.0.30319.42000'.</S><S S="verbose">Exporting function 'Format-FileSize'.</S><S S="verbose">Exporting function 'Get-ChecksumValid'.</S><S S="verbose">Exporting function 'Get-ChocolateyUnzip'.</S><S S="verbose">Exporting function 'Get-ChocolateyWebFile'.</S><S S="verbose">Exporting function 'Get-EnvironmentVariable'.</S><S S="verbose">Exporting function 'Get-EnvironmentVariableNames'.</S><S S="verbose">Exporting function 'Get-FtpFile'.</S><S S="verbose">Exporting function 'Get-OSArchitectureWidth'.</S><S S="verbose">Exporting function 'Get-PackageParameters'.</S><S S="verbose">Exporting function 'Get-PackageParametersBuiltIn'.</S><S S="verbose">Exporting function 'Get-ToolsLocation'.</S><S S="verbose">Exporting function 'Get-UACEnabled'.</S><S S="verbose">Exporting function 'Get-UninstallRegistryKey'.</S><S S="verbose">Exporting function 'Get-VirusCheckValid'.</S><S S="verbose">Exporting function 'Get-WebFile'.</S><S S="verbose">Exporting function 'Get-WebFileName'.</S><S S="verbose">Exporting function 'Get-WebHeaders'.</S><S S="verbose">Exporting function 'Install-BinFile'.</S><S S="verbose">Exporting function 'Install-ChocolateyDesktopLink'.</S><S S="verbose">Exporting function 'Install-ChocolateyEnvironmentVariable'.</S><S S="verbose">Exporting function 'Install-ChocolateyExplorerMenuItem'.</S><S S="verbose">Exporting function 'Install-ChocolateyFileAssociation'.</S><S S="verbose">Exporting function 'Install-ChocolateyInstallPackage'.</S><S S="verbose">Exporting function 'Install-ChocolateyPackage'.</S><S S="verbose">Exporting function 'Install-ChocolateyPath'.</S><S S="verbose">Exporting function 'Install-ChocolateyPinnedTaskBarItem'.</S><S S="verbose">Exporting function 'Install-ChocolateyPowershellCommand'.</S><S S="verbose">Exporting function 'Install-ChocolateyShortcut'.</S><S S="verbose">Exporting function 'Install-ChocolateyVsixPackage'.</S><S S="verbose">Exporting function 'Install-ChocolateyZipPackage'.</S><S S="verbose">Exporting function 'Install-Vsix'.</S><S S="verbose">Exporting function 'Set-EnvironmentVariable'.</S><S S="verbose">Exporting function 'Set-PowerShellExitCode'.</S><S S="verbose">Exporting function 'Start-ChocolateyProcessAsAdmin'.</S><S S="verbose">Exporting function 'Test-ProcessAdminRights'.</S><S S="verbose">Exporting function 'Uninstall-BinFile'.</S><S S="verbose">Exporting function 'Uninstall-ChocolateyEnvironmentVariable'.</S><S S="verbose">Exporting function 'Uninstall-ChocolateyPackage'.</S><S S="verbose">Exporting function 'Uninstall-ChocolateyZipPackage'.</S><S S="verbose">Exporting function 'Update-SessionEnvironment'.</S><S S="verbose">Exporting function 'Write-ChocolateyFailure'.</S><S S="verbose">Exporting function 'Write-ChocolateySuccess'.</S><S S="verbose">Exporting function 'Write-FileUpdateLog'.</S><S S="verbose">Exporting function 'Write-FunctionCallLogMessage'.</S><S S="verbose">Exporting alias 'Get-ProcessorBits'.</S><S S="verbose">Exporting alias 'Get-OSBitness'.</S><S S="verbose">Exporting alias 'Get-InstallRegistryKey'.</S><S S="verbose">Exporting alias 'Generate-BinFile'.</S><S S="verbose">Exporting alias 'Add-BinFile'.</S><S S="verbose">Exporting alias 'Start-ChocolateyProcess'.</S><S S="verbose">Exporting alias 'Invoke-ChocolateyProcess'.</S><S S="verbose">Exporting alias 'Remove-BinFile'.</S><S S="verbose">Exporting alias 'refreshenv'.</S><S S="verbose">Exporting function 'Format-FileSize'.</S><S S="verbose">Exporting function 'Get-ChecksumValid'.</S><S S="verbose">Exporting function 'Get-ChocolateyUnzip'.</S><S S="verbose">Exporting function 'Get-ChocolateyWebFile'.</S><S S="verbose">Exporting function 'Get-EnvironmentVariable'.</S><S S="verbose">Exporting function 'Get-EnvironmentVariableNames'.</S><S S="verbose">Exporting function 'Get-FtpFile'.</S><S S="verbose">Exporting function 'Get-OSArchitectureWidth'.</S><S S="verbose">Exporting function 'Get-PackageParameters'.</S><S S="verbose">Exporting function 'Get-PackageParametersBuiltIn'.</S><S S="verbose">Exporting function 'Get-ToolsLocation'.</S><S S="verbose">Exporting function 'Get-UACEnabled'.</S><S S="verbose">Exporting function 'Get-UninstallRegistryKey'.</S><S S="verbose">Exporting function 'Get-VirusCheckValid'.</S><S S="verbose">Exporting function 'Get-WebFile'.</S><S S="verbose">Exporting function 'Get-WebFileName'.</S><S S="verbose">Exporting function 'Get-WebHeaders'.</S><S S="verbose">Exporting function 'Install-BinFile'.</S><S S="verbose">Exporting function 'Install-ChocolateyDesktopLink'.</S><S S="verbose">Exporting function 'Install-ChocolateyEnvironmentVariable'.</S><S S="verbose">Exporting function 'Install-ChocolateyExplorerMenuItem'.</S><S S="verbose">Exporting function 'Install-ChocolateyFileAssociation'.</S><S S="verbose">Exporting function 'Install-ChocolateyInstallPackage'.</S><S S="verbose">Exporting function 'Install-ChocolateyPackage'.</S><S S="verbose">Exporting function 'Install-ChocolateyPath'.</S><S S="verbose">Exporting function 'Install-ChocolateyPinnedTaskBarItem'.</S><S S="verbose">Exporting function 'Install-ChocolateyPowershellCommand'.</S><S S="verbose">Exporting function 'Install-ChocolateyShortcut'.</S><S S="verbose">Exporting function 'Install-ChocolateyVsixPackage'.</S><S S="verbose">Exporting function 'Install-ChocolateyZipPackage'.</S><S S="verbose">Exporting function 'Install-Vsix'.</S><S S="verbose">Exporting function 'Set-EnvironmentVariable'.</S><S S="verbose">Exporting function 'Set-PowerShellExitCode'.</S><S S="verbose">Exporting function 'Start-ChocolateyProcessAsAdmin'.</S><S S="verbose">Exporting function 'Test-ProcessAdminRights'.</S><S S="verbose">Exporting function 'Uninstall-BinFile'.</S><S S="verbose">Exporting function 'Uninstall-ChocolateyEnvironmentVariable'.</S><S S="verbose">Exporting function 'Uninstall-ChocolateyPackage'.</S><S S="verbose">Exporting function 'Uninstall-ChocolateyZipPackage'.</S><S S="verbose">Exporting function 'Update-SessionEnvironment'.</S><S S="verbose">Exporting function 'Write-ChocolateyFailure'.</S><S S="verbose">Exporting function 'Write-ChocolateySuccess'.</S><S S="verbose">Exporting function 'Write-FileUpdateLog'.</S><S S="verbose">Exporting function 'Write-FunctionCallLogMessage'.</S><S S="verbose">Exporting alias 'Get-ProcessorBits'.</S><S S="verbose">Exporting alias 'Get-OSBitness'.</S><S S="verbose">Exporting alias 'Get-InstallRegistryKey'.</S><S S="verbose">Exporting alias 'Generate-BinFile'.</S><S S="verbose">Exporting alias 'Add-BinFile'.</S><S S="verbose">Exporting alias 'Start-ChocolateyProcess'.</S><S S="verbose">Exporting alias 'Invoke-ChocolateyProcess'.</S><S S="verbose">Exporting alias 'Remove-BinFile'.</S><S S="verbose">Exporting alias 'refreshenv'.</S></Objs>
0
Only an exit code of non-zero will fail the package by default. Set
`--failonstderr` if you want error messages to also fail a script. See
`choco -h` for details.
The install of yarn was successful.
Software installed as 'msi', install location is likely default.
Chocolatey installed 1/1 packages.
See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
C:\Windows\system32>
Finally, we can verify our installation of yarn using the following command.
C:\Windows\system32>yarn --version
1.22.4
C:\Windows\system32>
Scoop Installation
The third method of installing Yarn is by using the Scoop command-line installer for Windows. Scoop has functionality that is similar to Chocolatey, but the main difference being that Chocolatey will install node.js if we do not already have it installed but scoop will not. Scoop can be installed using the information found on the scoop website.
To install node.js using scoop, run the following command.
scoop install nodejs
Next, we can install yarn using this command.
scoop install yarn
Common Yarn Commands
The best method to review many of the Yarn commands is to run the help command.
yarn help
Start a New Project
To create a new project, run the following command.
yarn init
Installing Dependencies
To install all the dependencies for a project, use one of the two following commands.
yarn
yarn install
Add a Dependency
To add a dependency to a project, use one of the following command formats.
yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]
Add a Dependency to a Different Category of Dependencies
To add dependencies to a specific category, we can use one of the following three options:
- dev Dependencies
- peer Dependencies
- optional Dependencies
The syntax for adding these command dependencies is as follows.
yarn add [package] --dev # dev-dependency
yarn add [package] --peer # peer-dependency
yarn add [package] --optimal # optimal-dependency
Upgrade a Dependency
To upgrade an existing dependency, we can use the yarn up command.
yarn up [packagename]
yarn up [packagename]@[version]
yarn up [packagename]@[tag]
Remove a Dependency
To remove an existing dependency, we should use the following command.
yarn remove [package]
Upgrade Yarn
To upgrade Yarn to the latest version, run one of the following commands.
yarn set version latest
yarn set version from sources