当前位置: 首页 > 文档资料 > Yarn 中文文档 >

yarn version

优质
小牛编辑
131浏览
2023-12-01

Updates the package version.

Updating versions

Using the yarn version command you can update the version of your package via the command line.

For example, starting with this package.json package.json:

{
  "name": "example-yarn-package",
  "version": "1.0.1",
  "description": "An example package to demonstrate Yarn"
}

When we 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.

We will get this updated package.json:

{
  "name": "example-yarn-package",
  "version": "1.0.2",
  "description": "An example package to demonstrate Yarn"
}

Note: The new version you enter must be a valid SemVer version.

Git tags

If you run yarn version within a Git repository a Git tag will be created by default following the format v0.0.0.

You can customize the git tag that is created or disable this behavior by using yarn config set.

To change the prefix of the git tag you can use version-tag-prefix:

yarn config set version-tag-prefix "v"

Or you can change the git message using version-git-message where %s is the version string:

yarn config set version-git-message "v%s"

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 even enable or disable the git tagging behavior entirely by using version-git-tag:

yarn config set version-git-tag true

If you would like to stop git commit hooks from running, you can disable them using version-commit-hooks:

yarn config set version-commit-hooks false

Version lifecycle methods

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

  • yarn preversion
  • yarn version
  • yarn postversion

In these scripts you also get some handy environment variables, e.g. npm_package_version will in the preversion script hold the version before the version change, and in the postversion script it will hold the version after the version change.

This becomes useful when using yarn with git to publish new tags. Here is an example of what a package.json file could look like:

{
  "name": "example-yarn-package",
  "version": "1.0.2",
  "description": "An example package to demonstrate Yarn",
  "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 \"Successfully released version $npm_package_version!\""
  }
}

Running yarn version would 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
...
Successfully released version 2.0.0!
✨  Done in 2.72s.

After this both the remote repository should reflect the updated version and the package should be published under the same version.

Commands

yarn version

Create a new version using an interactive session to prompt you for a new version.

yarn version --new-version <version>

Creates a new version specified by <version>.

yarn version --major
yarn version --minor
yarn version --patch

Creates a new version by incrementing the major, minor, or patch number of the current version.

yarn version --no-git-tag-version

Creates a new version without creating a git tag.

yarn version --no-commit-hooks

Bypasses running commit hooks when committing the new version.