npm-version

修改包版本

选择命令行版本:

概要

🌐 Synopsis

npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
alias: verison

配置

🌐 Configuration

allow-same-version

  • 默认:否
  • 类型:布尔

当使用 npm version 将新版本设置为与当前版本相同的值时,可防止抛出错误。

🌐 Prevents throwing an error when npm version is used to set the new version to the same value as the current version.

commit-hooks

  • 默认:是
  • 类型:布尔

在使用 npm version 命令时运行 git 提交钩子。

🌐 Run git commit hooks when using the npm version command.

git-tag-version

  • 默认:是
  • 类型:布尔

在使用 npm version 命令时标记提交。将此设置为 false 则不会进行任何提交。

🌐 Tag the commit when using the npm version command. Setting this to false results in no commit being made at all.

json

  • 默认:否
  • 类型:布尔

是否输出 JSON 数据,而不是正常输出。

🌐 Whether or not to output JSON data, rather than the normal output.

  • npm pkg set 中,它可以在将值保存到你的 package.json 之前使用 JSON.parse() 解析设置的值。

并非所有 npm 命令都支持。

🌐 Not supported by all npm commands.

preid

  • 默认值:
  • 类型:字符串

用于作为 semver 中“预发布”部分前缀的“预发布标识符”。就像 1.2.0-rc.8 中的 rc

🌐 The "prerelease identifier" to use as a prefix for the "prerelease" part of a semver. Like the rc in 1.2.0-rc.8.

sign-git-tag

  • 默认:否
  • 类型:布尔

如果设置为 true,那么 npm version 命令将使用 -s 添加签名来标记版本。

🌐 If set to true, then the npm version command will tag the version using -s to add a signature.

请注意,git 要求你在 git 配置中设置 GPG 密钥才能正常工作。

🌐 Note that git requires you to have set up GPG keys in your git configs for this to work properly.

workspace

  • 默认值:
  • 类型:字符串(可以多次设置)

启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。

🌐 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

workspace 配置的有效值为以下之一:

🌐 Valid values for the workspace config are either:

  • 工作区名称
  • 工作区目录的路径
  • 父工作区目录的路径(将导致选择该文件夹中的所有工作区)

对于 npm init 命令设置时,可以将其设置为一个尚不存在的工作区文件夹,以创建该文件夹并将其作为项目内全新的工作区进行设置。

🌐 When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

workspaces

  • 默认值:空
  • 类型:空或布尔值

设置为 true 以在 所有 配置的工作区上下文中运行命令。

🌐 Set to true to run the command in the context of all configured workspaces.

将此显式设置为 false 会导致像 install 这样的命令完全忽略工作区。如果不显式设置:

🌐 Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

  • node_modules 树操作的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。- 执行其他操作的命令(测试、执行、发布等)会在根项目上运行,除非workspace 配置中指定了一个或多个工作区。

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

workspaces-update

  • 默认:是
  • 类型:布尔

如果设置为 true,npm CLI 将在可能更改安装到 node_modules 文件夹中的工作区的操作之后运行更新。

🌐 If set to true, the npm cli will run an update after operations that may possibly change the workspaces installed to the node_modules folder.

include-workspace-root

  • 默认:否
  • 类型:布尔

为命令启用工作区时包括工作区根。

🌐 Include the workspace root when workspaces are enabled for a command.

当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上操作,而不会在根项目上操作。

🌐 When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

描述

🌐 Description

在包目录中运行此命令以提升版本,并将新数据写回到 package.jsonpackage-lock.json,如果存在的话,还包括 npm-shrinkwrap.json

🌐 Run this in a package directory to bump the version and write the new data back to package.json, package-lock.json, and, if present, npm-shrinkwrap.json.

newversion 参数应是一个有效的 semver 字符串、semver.inc 的有效第二个参数(patchminormajorprepatchpreminorpremajorprerelease 之一),或 from-git。在第二种情况下,现有版本将在指定字段上增加 1。from-git 将尝试读取最新的 git 标签,并将其用作新的 npm 版本。

🌐 The newversion argument should be a valid semver string, a valid second argument to semver.inc (one of patch, minor, major, prepatch, preminor, premajor, prerelease), or from-git. In the second case, the existing version will be incremented by 1 in the specified field. from-git will try to read the latest git tag, and use that as the new npm version.

如果在 Git 仓库中运行,它还会创建一个版本提交和标签。此行为由 git-tag-version 控制(见下文),并且可以通过在命令行运行 npm --no-git-tag-version version 来禁用。如果工作目录不干净,它将失败,除非设置了 -f--force 标志。

🌐 If run in a git repo, it will also create a version commit and tag. This behavior is controlled by git-tag-version (see below), and can be disabled on the command line by running npm --no-git-tag-version version. It will fail if the working directory is not clean, unless the -f or --force flag is set.

如果提供了 -m--message 配置 选项,npm 在创建版本提交时将使用它作为提交信息。如果 message 配置包含 %s,那么它将被替换为生成的版本号。例如:

🌐 If supplied with -m or --message config option, npm will use it as a commit message when creating a version commit. If the message config contains %s then that will be replaced with the resulting version number. For example:

npm version patch -m "Upgrade to %s for reasons"

如果设置了 sign-git-tag 配置,则该标签将使用 git 的 -s 标志进行签名。请注意,你必须在 git 配置中设置默认 GPG 密钥,才能使其正常工作。例如:

🌐 If the sign-git-tag config is set, then the tag will be signed using the -s flag to git. Note that you must have a default GPG key set up in your git config for this to work properly. For example:

$ npm config set sign-git-tag true
$ npm version patch
You need a passphrase to unlock the secret key for
user: "isaacs (http://blog.izs.me/) <i@izs.me>"
2048-bit RSA key, ID 6C481CF6, created 2010-08-31
Enter passphrase:

如果 preversionversionpostversion 出现在 package.json 的 scripts 属性中,它们将在执行 npm version 时被运行。

🌐 If preversion, version, or postversion are in the scripts property of the package.json, they will be executed as part of running npm version.

具体执行顺序如下:

🌐 The exact order of execution is as follows:

  1. 在开始之前,请检查 git 工作目录是否干净。你的脚本可能会在后续步骤中向提交中添加文件。如果设置了 --force 标志,则此步骤将被跳过。
  2. 运行 preversion 脚本。这些脚本可以访问 package.json 中的旧 version。一个典型的用法是在部署前运行完整的测试套件。任何你希望添加到提交中的文件都应该使用 `git add`` 明确添加。
  3. 按要求在 package.json 中更新 versionpatchminormajor 等)。
  4. 运行 version 脚本。这些脚本可以访问 package.json 中的新 version(因此它们可以将其包含到生成文件的文件头中,例如)。同样,脚本应使用 git add 显式将生成的文件添加到提交中。
  5. 提交并标记。
  6. 运行 postversion 脚本。使用它来清理文件系统或自动提交更改和/或标签。

举个例子:

🌐 Take the following example:

{
"scripts": {
"preversion": "npm test",
"version": "npm run build && git add -A dist",
"postversion": "git push && git push --tags && rm -rf build/temp"
}
}

这会运行你所有的测试,并且仅在测试通过时继续。然后运行你的 build 脚本,并将 dist 目录中的所有内容添加到提交中。提交完成后,它会将新的提交和标签推送到服务器,并删除 build/temp 目录。

🌐 This runs all your tests and proceeds only if they pass. Then runs your build script, and adds everything in the dist directory to the commit. After the commit, it pushes the new commit and tag up to the server, and deletes the build/temp directory.

也可以看看

🌐 See Also