使用 npm-version
命令更新包的版本号是一个简单而直接的过程。这个命令不仅可以更新 package.json
文件中的版本号,还可以自动在 Git 中创建提交和标签。以下是如何使用 npm-version
命令更新包版本号的详细步骤:
前提条件
- 确保安装了 npm:确保你的系统上已经安装了 Node.js 和 npm。
- 在 Git 仓库中:
npm-version
命令通常用于 Git 仓库中,以便自动创建提交和标签。
使用步骤
-
打开终端:导航到你的项目目录。
-
选择版本更新类型:
- 指定具体版本号:如果你想直接更新到一个具体的版本号,可以使用:
npm version 1.2.3
- 使用语义化版本控制关键字:
major
:增加主版本号(例如,从1.2.3
到2.0.0
)。minor
:增加次版本号(例如,从1.2.3
到1.3.0
)。patch
:增加修订号(例如,从1.2.3
到1.2.4
)。premajor
、preminor
、prepatch
:用于创建预发布版本。prerelease
:增加预发布版本号(例如,从1.2.3-0
到1.2.3-1
)。
示例命令:
npm version minor
- 指定具体版本号:如果你想直接更新到一个具体的版本号,可以使用:
-
查看更新结果:
- 执行命令后,
package.json
文件中的版本号将被更新。 - 在 Git 仓库中,
npm-version
会自动创建一个新的提交,并将版本号作为提交信息。 - 一个新的 Git 标签将被创建,标签名为
v
加上新的版本号(例如,v1.2.4
)。
- 执行命令后,
自定义提交信息
如果你想自定义提交信息,可以使用 -m
或 --message
参数:
npm version patch -m "Release version %s"
其中 %s
将被替换为新的版本号。
跳过 Git 提交和标签
如果你不想自动创建 Git 提交和标签,可以使用 --no-git-tag-version
参数:
npm version patch --no-git-tag-version
强制更新
如果工作目录中有未提交的更改,npm-version
会拒绝执行。你可以使用 -f
或 --force
参数强制执行:
npm version patch -f
注意事项
- 确保工作目录干净:在执行
npm-version
命令之前,确保没有未提交的更改,除非使用--force
。 - 检查
package.json
:确保package.json
文件中有一个有效的version
字段。 - 配置 npm:可以通过
npm config set
来设置一些配置选项,例如sign-git-tag
。
通过这些步骤,你可以轻松地使用 npm-version
命令来更新你的包版本号,并自动化版本发布流程。
npm-version
是 npm(Node Package Manager)中的一个命令,用于更新包(package)的版本号,并自动更新 package.json
文件中的版本号信息。它遵循语义化版本控制(Semantic Versioning,简称 SemVer)的规范。以下是 npm-version
的详细解释和使用方法:
基本用法
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
<newversion>
:指定新的版本号,例如1.2.3
。major
:将主版本号加 1,次版本号和修订号重置为 0。例如,从1.2.3
更新到2.0.0
。minor
:将次版本号加 1,修订号重置为 0。例如,从1.2.3
更新到1.3.0
。patch
:将修订号加 1。例如,从1.2.3
更新到1.2.4
。premajor
:将主版本号加 1,次版本号和修订号重置为 0,并在版本号后加上-0
作为预发布标识符。例如,从1.2.3
更新到2.0.0-0
。preminor
:将次版本号加 1,修订号重置为 0,并在版本号后加上-0
作为预发布标识符。例如,从1.2.3
更新到1.3.0-0
。prepatch
:将修订号加 1,并在版本号后加上-0
作为预发布标识符。例如,从1.2.3
更新到1.2.4-0
。prerelease
:将预发布标识符增加 1,例如从1.2.3-0
更新到1.2.3-1
。from-git
:尝试读取最新的 Git 标签,并将其作为新的 npm 版本号。
使用示例
-
直接指定版本号
npm version 1.2.3
执行后,
package.json
文件中的版本号将更新为1.2.3
。 -
使用关键字更新版本
npm version patch
如果当前版本是
1.2.3
,执行后版本号将更新为1.2.4
。 -
创建预发布版本
npm version prerelease
如果当前版本是
1.2.3
,执行后版本号将更新为1.2.4-0
。再次执行将更新为1.2.4-1
。
自动提交和标签
在 Git 仓库中运行 npm version
命令时,它会自动创建一个新的提交,提交消息为新的版本号,并在 Git 上打一个标签。例如,如果当前版本是 1.2.3
,执行 npm version patch
后,Git 将创建一个提交,提交消息为 1.2.4
,并打上一个名为 v1.2.4
的标签。
自定义提交消息
您可以使用 -m
或 --message
参数来自定义提交消息。例如:
npm version patch -m "Release version %s"
其中 %s
将被替换为新的版本号。
注意事项
- 在执行
npm version
命令之前,请确保 Git 工作目录是干净的,即没有任何未提交的更改。 - 如果需要跳过此检查,可以使用
-f
或--force
参数。 - 可以通过在命令行上运行
npm --no-git-tag-version version
来禁用自动提交和打标签的行为。
配置选项
npm-version
还支持一些配置选项,可以通过 npm config set
命令来设置。例如:
sign-git-tag
:如果设置为true
,则使用 Git 的-s
标志为标签添加签名。git-tag-version
:如果设置为false
,则不创建 Git 提交和标签。
总结
npm-version
是一个强大的工具,用于自动化包版本的管理和发布流程。它简化了版本更新的过程,并确保版本号的更新与代码库的提交和标签保持同步。
Bump a package version
npm version [ | major | minor | patch | premajor | preminor | prepatch | prerelease [–preid=] | from-git]
‘npm [-v | --version]’ to print npm version
‘npm view version’ to view a package’s published version
‘npm ls’ to inspect current package/dependency versions
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.
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.
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.
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”
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:
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:
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.
Run the preversion script. These scripts have access to the old version 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 using git add.
Bump version in package.json as requested (patch, minor, major, etc).
Run the version script. These scripts have access to the new version 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 using git add.
Commit and tag.
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”
}
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.
Configuration
allow-same-version
Default: false
Type: Boolean
Prevents throwing an error when npm version is used to set the new version to the same value as the current version.
git-tag-version
Default: true
Type: Boolean
Commit and tag the version change.
commit-hooks
Default: true
Type: Boolean
Run git commit hooks when committing the version change.
sign-git-tag
Default: false
Type: Boolean
Pass the -s flag to git to sign the tag.
Note that you must have a default GPG key set up in your git config for this to work properly.