w3resource

Yarn version and yarn versions CLI commands


In this tutorial we will show you how to update a package's version, we will also show you how to display the version information for the currently installed yarn, Node.js and its dependencies.

yarn version

The yarn version command will update the package version.

Updating versions

When you run the yarn version command from the command line, it will update the version of your package.

For instance, if you start with this package.json:

{
  "name": "sample-yarn-package",
  "version": "1.0.1"
}

If you run the yarn version command:

yarn version

info Current version: 1.0.1
question New version: 1.0.2
info New version: 1.0.2
 Done in 9.42s.

You will get this updated package.json:

{
  "name": "sample-yarn-package",
  "version": "1.0.2"
}

Git tags

When you run yarn version within a Git repository, an annotated Git tag will be created by default following the format vx.x.x.

The git tag that is created can be customized or disable this behavior when you use yarn config set.

If you want to change the prefix of the tag, you can use version-tag-prefix:

yarn config set version-tag-prefix "v"

or you can also turn signing git tags on or off using version-sign-git-tag:

yarn config set version-sign-git-tag false

You can also enable or disable the git tagging entirely by usingversion-git-tag:

yarn config set version-git-tag true

In the case where you want to stop git commit hooks from running, you will be able to disable them using version-commit-hooks:

yarn config set version-commit-hooks false	

Version lifecycle methods

When you run the yarn version command, it will also run the usual lifecycle methods in the following order:

  • yarn preversion
  • yarn version
  • yarn postversion

You will also get some handy environment variables, e.g npm_package_version will hold the version in the perversion script before the version change, and will hold the version in the postversion script after the version change.

This is very useful when you are using yarn with git to publish new tags. An example of what a package.json file could look like is shown below:

{
  "name": "sample-yarn-package",
  "version": "1.0.2",
    "scripts": {
    "test": "echo \"Running tests for version $npm_package_version...\"",
    "preversion": "yarn test",
    "postversion":
      "git push --tags && yarn publish . --tag $npm_package_version && git push && echo \" version $npm_package_version was successfully released!\""
  }
}

When you run yarn version, it will look something like this:

info Current version: 1.0.2
Running tests for version 1.0.2...
 Done in 0.10s.
info New version: 2.0.0

To github.com:example-org/example-yarn-package.git

 * [new tag]             v2.0.0 -> v2.0.0
version 2.0.0 was successfully released!
 Done in 2.72s.

Once you are done with this, the remote repository will have to reflect the updated version and the package has to be published under the same version.

Commands

yarn version

This will create a new version using an interactive session to prompt you for a new version.

yarn version --new-version <version>

When you run this command, it will create a new version as specified by <version>

yarn version --major

yarn version --minor

yarn version --patch

This command will create a new version by incrementing the major, minor, or patch number of the current version.

yarn version --premajor

yarn version --preminor

yarn version --prepatch

This will create a new prerelease version by incrementing the major, minor, or patch number of the current version and adding a prerelease number.

yarn version --prerelease

This command will increment the prerelease version number keeping the main version.

yarn version [--premajor | --preminor | --prepatch | --prerelease] --preid <pre-identifier>

This command will add an identifier specified by <pre-identifier> to be used to prefix premajor, preminor, prepatch or prerelease version increments.

yarn version --no-git-tag-version

This command will create a new version without creating a git tag.

yarn version --no-commit-hooks

The yarn version ?no-commit-hooks will bypass running commit hooks when committing the new version.

Yarn versions

The yarn versions command will information of the currently installed Yarn, Node.js, and its dependencies.

Here is an example screenshot:

Yarn versions

Previous: Yarn upgrade-interactive CLI command
Next: Yarn why and yarn workspaces CLI commands



Follow us on Facebook and Twitter for latest update.