w3resource

The yarn pack and policies CLI commands


In this tutorial, we are going to teach you how to create a compressed gzip archive of your package dependencies. We will also show you how to define a project wide policy for your project.

yarn pack

To create a compressed gzip archive of your package dependencies, you have to run yarn pack from the command line of your project's directory.

Here is an example:

yarn pack

The command above creates a gzip archive of the entire tic-tac project.

yarn pack --filename <filename>

This will create a compressed gzip archive of the tic-tac project.

yarn pack  --completed tic-tac

yarn pack v1.21.1

success Wrote tarball to "C:\\Users\\Awam Victor\\Desktop\\Created Works\\Programs\\tic-tac\\tic-tac-v0.1.0.tgz".

Done in 0.17s.

yarn policies

This command defines the project-wise policies for your project.

Enforcing Yarn's version across your project

While working in a team, there are cases where one colleague is using a different version of than you. This might seem inconsequential, but it could lead to also lead to subtle and hard to debug issues- for instance, the version of Yarn your colleague is using might not support a feature that you rely on.

To solve this issue, you will need to specify the required version of yarn in your package.json file, which prompts your colleagues to use the same version as you.

Although this is a technically sound technique, it also adds an unwanted burden on them- since yarns's binary is typically global, to switch from a branch to another could be very annoying.

Here is a fix as proposed by yarn:

Run the yarn policies set-version, this command offers you a simple way to check in your yarn release within your repository. When this command is run, it will update your configuration in such a way that anyone running a yarn command inside the project will always use the version that you set and it does this transparently.

This command accepts various ways to set the version you want to use:

  • yarn policies set-version downloads the latest stable release
  • yarn policies set-version --rc downloads the latest rc release
  • yarn policies set-version 1.13.0 downloads a specific version
  • yarn policies set-version '^1.12.0' downloads the latest minor

This command simply downloads the single-file release from the GitHub repository, store it in your project (in a .yarn/release folder), then it will finally update your configuration to point to the new file (with the use of yarn-path).

The yarn policies set-version is the preferred way to upgrade yarn.

Previous: Yarn outdated and yarn owner CLI commands
Next: Yarn prune, yarn publish and yarn remove CLI commands