目录
选择命令行版本:
目录
概要
¥Synopsis
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]alias: verison
配置
¥Configuration
allow-same-version
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
防止在使用 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
-
默认值:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
使用 npm version
命令时运行 git commit hooks。
¥Run git commit hooks when using the npm version
command.
git-tag-version
-
默认值:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
使用 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
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
是否输出 JSON 数据,而不是正常输出。
¥Whether or not to output JSON data, rather than the normal output.
-
在
npm pkg set
中,它可以使用 JSON.parse() 解析集合值,然后再将它们保存到你的package.json
。¥In
npm pkg set
it enables parsing set values with JSON.parse() before saving them to yourpackage.json
.
并非所有 npm 命令都支持。
¥Not supported by all npm commands.
preid
-
默认值:""
¥Default: ""
-
类型:字符串
¥Type: String
"预发布标识符" 用作 semver 的 "prerelease" 部分的前缀。就像 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
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
如果设置为 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
-
默认值:
¥Default:
-
类型:字符串(可以设置多次)
¥Type: String (can be set multiple times)
启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。
¥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:
-
工作区名称
¥Workspace names
-
工作区目录的路径
¥Path to a workspace directory
-
父工作区目录的路径(将导致选择该文件夹中的所有工作区)
¥Path to a parent workspace directory (will result in selecting all workspaces within that folder)
为 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
-
默认值:null
¥Default: null
-
类型:空值或布尔值
¥Type: null or Boolean
设置为 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
配置中指定了一个或多个工作区。¥Commands that operate on the
node_modules
tree (install, update, etc.) will link workspaces into thenode_modules
folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in theworkspace
config.
此值不会导出到子进程的环境中。
¥This value is not exported to the environment for child processes.
workspaces-update
-
默认值:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
如果设置为 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
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
为命令启用工作区时包括工作区根。
¥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.json
、package-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 的有效第二个参数(patch
、minor
、major
、prepatch
、preminor
、premajor
、prerelease
之一)或 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 repo 中运行,它还将创建一个版本提交和标签。此行为由 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
配置,那么标签将使用 -s
标志对 git 进行签名。请注意,你必须在 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 patchYou need a passphrase to unlock the secret key foruser: "isaacs (http://blog.izs.me/) <i@izs.me>"2048-bit RSA key, ID 6C481CF6, created 2010-08-31Enter passphrase:
如果 preversion
、version
或 postversion
在 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:
-
在我们开始之前检查以确保 git 工作目录是干净的。你的脚本可能会在以后的步骤中将文件添加到提交中。如果设置了
--force
标志,则跳过此步骤。¥Check to make sure the git working directory is clean before we get started. Your scripts may add files to the commit in future steps. This step is skipped if the
--force
flag is set. -
运行
preversion
脚本。这些脚本可以访问 package.json 中的旧version
。一个典型的用途是在部署之前运行你的完整测试套件。你要添加到提交中的任何文件都应使用git add
显式添加。¥Run the
preversion
script. These scripts have access to the oldversion
in package.json. A typical use would be running your full test suite before deploying. Any files you want added to the commit should be explicitly added usinggit add
. -
根据要求(
patch
、minor
、major
等)在package.json
中撞击version
。¥Bump
version
inpackage.json
as requested (patch
,minor
,major
, etc). -
运行
version
脚本。这些脚本可以访问 package.json 中的新version
(例如,它们可以将其合并到生成文件的文件头中)。同样,脚本应该使用git add
显式地将生成的文件添加到提交中。¥Run the
version
script. These scripts have access to the newversion
in package.json (so they can incorporate it into file headers in generated files for example). Again, scripts should explicitly add generated files to the commit usinggit add
. -
提交并标记。
¥Commit and tag.
-
运行
postversion
脚本。使用它来清理文件系统或自动推送提交和/或标签。¥Run the
postversion
script. Use it to clean up the file system or automatically push the commit and/or tag.
举个例子:
¥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